In this lesson, you will learn.
The java.io package provides a rich set of classes to perform input and output (I/O) operations. Java categorizes these stream classes into two main types based on the data they handle.

readLine() method. The BufferedWriter provides buffering for an output character stream, improving writing efficiency. It also provides a newLine() method for writing platform-specific line separators.In this lesson, you will learn Character stream classes, i.e, FileWriter class to write data to a file.
OutputStreamWriter and assumes the default character encoding of the system.write() methods to write single characters, character arrays, or strings to a file.
The four commonly used constructors are shown here:
1. FileWriter(String fileName): Creates a FileWriter object given the name of the file. It will overwrite the content of the file if it exists.
2. FileWriter(String fileName, boolean append): Creates a FileWriter object given a file name with an option to append. If append is true, then data will be written to the end of the file rather than the beginning.
3. FileWriter(File file): Creates a FileWriter object given a File object. This constructor will also overwrite the existing file.
4. FileWriter(File file, boolean append): Similar to the second constructor but uses a File object. It allows appending if the boolean append is set to true.

package filewriting;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample1 {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("src/filewriting/example1.txt");
writer.write("Hello, this is text written to a file.");
writer.write("\nNandini");
writer.write("\nRam");
System.out.println("The file writing operation completed!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The file writing operation completed!
This constructor overwrites example.txt if it exists or creates a new file if it doesn’t.

package filewriting;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample2 {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("src/filewriting/example2.txt",true);
writer.write("nAppending this line to the file.");
writer.write("nNandini");
writer.write("nRam");
System.out.println("The file writing operation completed!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The file writing operation completed!
This code appends text to the existing example2.txt file. If the file does not exist, it creates a new file.

package filewriting;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample3 {
public static void main(String[] args) {
File file = new File("src/filewriting/example3.txt");
try {
FileWriter writer = new FileWriter(file);
writer.write("Writing to a file using File object.");
System.out.println("The file writing operation completed!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This approach uses a File object to specify the file path, which is useful when the file object is already available from previous operations.
The notes are concise, well-summarized, and perfect for quick revision before exams or interviews and youtube tutorials are also helpful . watching tutorial and revision here make java easy .
You must be logged in to submit a review.