0% found this document useful (0 votes)
0 views

File Writing in Java

The document outlines various Java classes for writing to .txt files, including FileWriter, BufferedWriter, FileOutputStream, and BufferedOutputStream, each suited for different types of content. It details the prerequisites for file writing, example usage for each class, and when to use them based on content size and performance needs. Additionally, it compares the advantages and disadvantages of each method.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

File Writing in Java

The document outlines various Java classes for writing to .txt files, including FileWriter, BufferedWriter, FileOutputStream, and BufferedOutputStream, each suited for different types of content. It details the prerequisites for file writing, example usage for each class, and when to use them based on content size and performance needs. Additionally, it compares the advantages and disadvantages of each method.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File Writing in Java

When we want to write something to a .txt file using Java, we have multiple
ways to do it. Below are the most commonly used predefined classes for
writing content to a text file:

1. FileWriter (For simple text writing)


2. BufferedWriter (For writing large content efficiently)
3. FileOutputStream (For writing binary data)
4. BufferedOutputStream (For efficient binary data writing)

Each of these classes has its own advantages and disadvantages.

Prerequisites for Writing to a File

Before writing content to a .txt file, we need to know:

1. The location of the file.


2. The content that we want to write.

Both the file location and the content are represented as Strings in Java:

String locationOfFile = "file.txt"; // If no path is provided, the file will create inside the
project folder.

1. FileWriter (For Simple Text Writing)


FileWriter is a predefined class in Java used for writing character data to a file.
It is best used for writing single-line content.

Example Usage

String content = "Hello Yogesh, this is a single-line text.";


FileWriter fileWriter = new FileWriter(locationOfFile);
fileWriter.write(content);
fileWriter.close();
When to Use FileWriter?

• When writing small content (single-line or short messages).


• When performance is not a major concern.

2. BufferedWriter (For Large Text Content)


BufferedWriter is an efficient way to write large text files. Instead of writing
directly to the file, it stores content in a temporary buffer and writes it in
chunks, reducing disk I/O operations.

Example Usage

String largeContent = "This is the first line.\n" +


"This is the second line.\n" +
"This is the third line.\n";

FileWriter fileWriter = new FileWriter(locationOfFile);


BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(largeContent);
bufferedWriter.close();

When to Use BufferedWriter?

• When writing large text content (multiple lines).


• When performance is a concern.

3. FileOutputStream (For Binary Data Writing)


FileOutputStream is used for writing binary data such as images, audio, or raw
byte content to a file. Instead of writing characters, it writes raw byte data.
Example Usage

String content = "BinaryDataExample";


byte[] contentInByteFormat = content.getBytes();

FileOutputStream fileOutputStream = new FileOutputStream(locationOfFile);


fileOutputStream.write(contentInByteFormat);
fileOutputStream.close();

When to Use FileOutputStream?

• When writing binary data (e.g., images, audio files, etc.).


• When performance is a concern, as byte streams can be faster for
certain operations.

4. BufferedOutputStream (For Efficient Binary Data


Writing)
BufferedOutputStream improves performance when writing binary data by
using an internal buffer to store data before writing it to the file, reducing
frequent I/O operations.

Example Usage

String content = "Buffered binary data example";


byte[] contentInByteFormat = content.getBytes();

FileOutputStream fileOutputStream = new FileOutputStream(locationOfFile);


BufferedOutputStream bufferedOutputStream = new
BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write(contentInByteFormat);
bufferedOutputStream.close();

When to Use BufferedOutputStream?

• When writing large binary files efficiently.


• When reducing direct interaction with the file to improve performance.
Best Used
Method Key Advantage Key Disadvantage
For
Writing small
FileWriter Simple to use Slow for large files
text content
Writing large Uses buffering,
Requires an additional
BufferedWriter text files better
FileWriter object
efficiently performance
Writing
binary data Works with Not suitable for
FileOutputStream
(images, byte streams character data
audio)
Writing large Uses buffering, Requires an additional
BufferedOutputStream binary files better FileOutputStream
efficiently performance object

You might also like