Advertisement
BufferedWriter(Writer r) |
---|
This constructor creates a BufferedWriter object to write characters out of a file, accessed by a Writer
Example-:
|
Methods | Description |
---|---|
flush() | This method flushes any data to be written out of this character output stream's buffer. |
write(int c) | This method writes one character at a time to this character output stream's buffer. |
write(char[]) | This method writes a whole character array at a time to this character output stream's buffer. |
write(String str) | This method writes a String to this character output stream's buffer. |
newLine() | This method writes a line separator. |
close() | This method closes this output stream and also frees any resources connected with this output stream. |
Advertisement
//A program to create a buffered output Stream for writing characters.
import java.io.*;
class A
{
public static void main(String... ar)
{
char[] arr= {'H', 'e', 'l' , 'l' , 'o'};
String str="Next topic is PrintWriter";
try
{
File file= new File("TextBook9.txt");
FileWriter fw= new FileWriter(file); // This will create the file and we don't need to call createNewFile();
BufferedWriter bw = new BufferedWriter(fw);
for(char ch : arr)
bw.write(ch); //Writing a char at a time, using for-each loop
bw.newLine(); //Writing a newline character.
bw.write(str); ////Writing a String.
bw.flush();
bw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement