Last Updated : 16 Aug, 2022
Bufferreader class writes text to character-output stream, buffering characters.Thus, providing efficient writing of single array, character and strings. A buffer size needs to be specified, if not it takes Default value.
An output is immediately set to the underlying character or byte stream by the Writer.
Class Declaration
public class BufferedWriter extends Writer
Constructors
Methods:
public void write(int arg) Parameters : arg : integer that specifies the character to write Return : Doesn't return any value.
//Java program illustrating use of write(int arg) method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
//initializing FileWriter
FileWriter geek_file;
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(geek_file);
System.out.println("Buffered Writer start writing :)");
// Use of write() method to write the value in 'ABC' file
// Printing E
geekwrite.write(69);
// Printing 1
geekwrite.write(49);
// Closing BufferWriter to end operation
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
Output : Buffered Writer start writing :) Written successfully
public void write(String arg, int offset, int length) Parameters : arg : String to be written offset : From where to start reading the string length : No. of characters of the string to write Return : Doesn't return any value.
//Java program illustrating use of write(String arg, int offset, int length) method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
//Initializing a FileWriter
FileWriter geek_file;
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing a BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(geek_file);
System.out.println("Buffered Writer start writing :)");
String arg = "Hello Geeks";
int offset = 6;
geekwrite.write(arg,offset,arg.length()-offset);
// Closing Buffer
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
arg = Hello Geeks offset = 6 length = arg.length So, when we minus offset : 6, it will write 'Geeks' only in the file.
Buffered Writer start writing :) Written successfully
public void newLine() Return : Doesn't return any value.
//Java program explaining use of newLine() method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
//initializing FileWriter
FileWriter geek_file;
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(geek_file);
System.out.println("Buffered Writer start writing :)");
// Use of write() method to write the value in 'ABC' file
// Printing "GEEKS"
geekwrite.write("GEEKS");
// For next line
geekwrite.newLine();
// Printing "FOR"
geekwrite.write("FOR");
// For next line
geekwrite.newLine();
// Printing "GEEKS"
geekwrite.write("FOR");
// Closing BufferWriter to end operation
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
Buffered Writer start writing :) Written successfully
public void flush() Return : Doesn't return any value.
public void close() Return : Doesn't return any value.
//Java program illustrating use of flush(), close() method
import java.io.*; //BufferedWriter, FileWriter, IOException
public class NewClass
{
public static void main(String[] args)
{
FileWriter geek_file; //initializing FileWriter
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(geek_file);
System.out.println("Buffered Writer start writing :)");
// Use of write() method to write the value in 'ABC' file
geekwrite.write(69); // Printing E
geekwrite.newLine(); // For next line
geekwrite.write(49); // Printing 1
// flush() method : flushing the stream
geekwrite.flush();
// close() method : closing BufferWriter to end operation
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
Output : Buffered Writer start writing :) Written successfully
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4