import java.io.*;
public class Main {
public static void main(String[] args) {
try {String str = "SomeMoreTextIsHere";File newTextFile = new File("C:/thetextfile.txt");
FileWriter fw = new FileWriter(newTextFile);fw.write(str);fw.close();
} catch (IOException iox) {//do stuff with exceptioniox.printStackTrace();}}}
import java.io.*;import java.util.*;
class WriteText{public static void main(String[] args){try {String text = "Your sample content to save in a text file.";BufferedWriter out = new BufferedWriter(new FileWriter("sample.txt"));out.write(text);out.close();}catch (IOException e){System.out.println("Exception ");}
return ;}};
// This is where the file goes.File destination = new File("file.txt");// This line isn't needed, but is really useful// if you're a beginner and don't know where your file is going to end up.System.out.println(destination.getAbsolutePath());try {Files.write(text, destination, Charset.forName("UTF-8"));} catch (IOException e) {// Useful error handling here}
import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths;
public class StringWriteSample {public static void main(String[] args) {String text = "This is text to be saved in file";
try {Files.write(Paths.get("my-file.txt"), text.getBytes());} catch (IOException e) {e.printStackTrace();}}}