This example can easily write a string to a file. You can try it. This is a code I learned from a foreign friend. You can learn from it.
Copy the code code as follows:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class StringToFile {
public static void main(String[] args) throws IOException {
String msg = "hello";
Files.write(Paths.get("./duke.txt"), msg.getBytes());
}
}