Java BufferedWriter class is used to write text to a character-output stream. This class is used as a wrapper around any Writer (FileWriter and OutputStreamWriter) whose write() operations may be costly. java.io.BufferedWriter
makes the write operation more efficient by buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that
would then be written immediately to the file, which can be very inefficient.
Java BufferedWriter constructors
- BufferedWriter(Writer out)- Wraps the passed Writer and creates a buffering character-input stream that uses a default-sized input buffer. Default buffer size for BufferedWriter is 8192 bytes i.e. 8 KB.
For example-
BufferedWriter out = new BufferedWriter(new FileWriter("resources/abc.txt"));
- BufferedWriter(Writer out, int sz)- Wraps the passed Writer and creates a new buffered character-output stream that uses an output buffer of the given size.
Java BufferedWriter methods
Methods in BufferedWriter class are as given below-
- flush()- Flushes the stream.
- newLine()- Writes a line separator.
- write(int c)- Writes a single character.
- write(char[] cbuf, int off, int len)- Writes a portion of an array of characters. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.
- write(String s, int off, int len)- Writes a portion of a String. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.
Java BufferedWriter examples
For using BufferedWriter steps are as follows-
- Create an instance of BufferedWriter wrapped around another Writer.
- Using that instance to write to a file.
- Close the stream, you should close the resources in finally block or you can use try-with-resources to automatically manage resources.
1. Using write(int c)
method of the Java BufferedWriter class to write single character to a file.
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class FileWriteRW { public static void main(String[] args) throws IOException { try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){ bw.write(65); //writes A bw.write(66); //writes B bw.write(67); //writes C } } }
2. Using write(char[] cbuf, int off, int len)
method of the Java BufferedWriter class to write portion of a character array.
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class FileWriteRW { public static void main(String[] args) throws IOException { String str = "This is a test String"; char[] buffer = str.toCharArray(); try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){ bw.write(buffer, 0, 7);// takes portion from index 0..6 } } }
3. Using write(String s, int off, int len)
of the Java BufferedWriter class to write a portion of a String.
public class FileWriteRW { public static void main(String[] args) throws IOException { String str = "This is a test String"; try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){ bw.write(str, 0, 7);// takes substring from index 0..6 } } }
4. Using newLine()
method of the Java BufferedWriter class.
public class FileWriteRW { public static void main(String[] args) throws IOException { String str = "This is a test String"; try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){ bw.write(str, 0, 7);// takes substring from index 0..6 bw.newLine(); // new line bw.write(str, 7, str.length()-7); bw.flush(); // flushes the stream } } }
That's all for this topic Java BufferedWriter Class With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Advanced Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment