Introduction
The BufferedWriter
class is a subclass of Writer
and is designed to write text to an output stream by buffering characters. Buffering can improve the performance of I/O operations by reducing the number of physical writes to the underlying output source. The BufferedWriter
writes chunks of data to a buffer, which is then written to the output stream as needed.
Table of Contents
- Introduction
- Creating a BufferedWriter
- Writing Data to a BufferedWriter
- Flushing the Stream
- Closing the Stream
- Complete Example
- Conclusion
Creating a BufferedWriter
To create a BufferedWriter
, you need to wrap it around another Writer
, such as a FileWriter
. You can also specify the size of the buffer, although a default size is provided if you do not specify one.
Example
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try {
// Creating BufferedWriter using FileWriter
FileWriter fw = new FileWriter("example.txt");
BufferedWriter bw = new BufferedWriter(fw);
// Creating BufferedWriter with specified buffer size
BufferedWriter bwWithBufferSize = new BufferedWriter(fw, 8192);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, BufferedWriter
is created using a FileWriter
. Additionally, a BufferedWriter
with a specified buffer size of 8192 characters is also created.
Writing Data to a BufferedWriter
The BufferedWriter
class provides several methods to write data to the output stream:
void write(int c)
: Writes a single character.void write(char[] cbuf, int off, int len)
: Writes a portion of an array of characters.void write(String s, int off, int len)
: Writes a portion of a string.void newLine()
: Writes a newline character.
Example
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterWriteExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"))) {
String data = "Hello, World!";
bw.write(data);
bw.newLine();
System.out.println("Data written to file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, BufferedWriter
writes data to a file named example.txt
. The data is written as a string followed by a newline character.
Flushing the Stream
It is important to flush the BufferedWriter
to ensure that all buffered data is written to the underlying output stream. This can be done using the flush()
method.
Example
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterFlushExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"))) {
String data = "Hello, World!";
bw.write(data);
bw.newLine();
bw.flush();
System.out.println("Data flushed and written to file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, the flush()
method is called to ensure that all buffered data is written to the file.
Closing the Stream
It is important to close the BufferedWriter
after completing the file operations to release the system resources associated with the stream. This can be done using the close()
method.
Example
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterCloseExample {
public static void main(String[] args) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("example.txt"));
String data = "Hello, World!";
bw.write(data);
bw.newLine();
System.out.println("Data written to file successfully.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
In the example above, the BufferedWriter
is closed in the finally
block to ensure that it is closed even if an exception occurs.
Alternatively, you can use the try-with-resources statement, which ensures that the stream is closed automatically.
Example
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterTryWithResourcesExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"))) {
String data = "Hello, World!";
bw.write(data);
bw.newLine();
System.out.println("Data written to file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, the try-with-resources statement is used to automatically close the BufferedWriter
.
Complete Example
Here is a complete example demonstrating the creation, writing, flushing, and closing of a BufferedWriter
.
BufferedWriterExample.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"))) {
String data = "Hello, World!";
bw.write(data);
bw.newLine();
bw.flush();
System.out.println("Data written and flushed to file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, a BufferedWriter
is created, data is written to a file, the stream is flushed, and the stream is automatically closed using the try-with-resources statement.
Conclusion
The BufferedWriter
class in Java is used for improving the performance of output operations by buffering the output data. By understanding how to create, write, flush, and close a BufferedWriter
, you can effectively handle file I/O operations involving large amounts of text data in your Java applications. Remember to always close the stream after use to ensure that system resources are properly released.
Comments
Post a Comment
Leave Comment