-3

First I like..

What is simplest way to read a file into String?

String content = new Scanner(new File("H:MM-Full-AF.txt")).useDelimiter("\\Z").next();
System.out.println(content);

But then I was like.. What be the inverse?

Community
  • 1
  • 1
user3025996
  • 47
  • 1
  • 1
  • 4

1 Answers1

0

Below code can do what you want if you don't want to use any additional libraries like apache commons. However you may have to put some try catch around it.

FileWriter fileWriter = null;
String content = "A string to be written to file";
File newTextFile = new File("C:\\output.txt");
fileWriter = new FileWriter(newTextFile);
fileWriter.write(content);
fileWriter.close();

This is another way of doing the same...

BufferedWriter out = new BufferedWriter(new FileWriter("C:\\output.txt""));
out.write("A string to be written to file");
out.close();
Buddha
  • 4,096
  • 2
  • 22
  • 49