To write a string of text on a file we have to do the following:
The Java statements that realize the three phases described above are:
// 1. opening the file for writing (creation of the file) FileWriter f = new FileWriter("test.txt"); PrintWriter out = new PrintWriter(f); // 2. writing text on the file out.println("some text to write to the file"); // 3. closing the output channel and the file out.close(); f.close();
Notes: