Java Data Type How to - Write a String to a text file








Question

We would like to know how to write a String to a text file.

Answer

import java.io.PrintWriter;
public class Main {
    public static void main(String[] args) throws Exception {
        String str = "???????"; 
        try (PrintWriter out = new PrintWriter("output.txt", "UTF-8")) {
            out.write(str);
        }
    }
}