bufferedwriter and BufferedReader output input streams for characters with default buffers, because there is a buffer so it is more efficient than no buffer. First, BufferedWriter class
construction Method: bufferedwriter bf = new BufferedWriter (Writer out);
Main method: void Write (char ch);//write a single character.
void Write (char []cbuf,int off,int Len) writes a portion of the character data.
void Write (string s,int off,int len) is written to a part of the string.
void newline ()//writes a row separator.
void Flush ();//Flushes the buffer in the stream. Writes the buffered data to the destination file.
void Close ();//closes this stream, and refreshes him before closing.
Package buffered;
Import Java.io.BufferedWriter;
Import Java.io.FileWriter;
Import java.io.IOException;
public class Bufferedwriterdemo {public
static void Main (string[] args) throws IOException {
FileWriter fw = new FileWriter ("Buffered.txt"); fw.write ("ok168"); fw.close ();
/**
* In order to improve the efficiency of writing, a character stream buffer is used.
* Creates a buffer object with a character write inflow and is associated with a stream object that specifies the buffer to be buffered.
*/
BufferedWriter BUFW = new BufferedWriter (FW);
Writes data to a buffer using a method in the buffer.
bufw.write ("Hello World!");
Bufw.newline ();
Bufw.newline ();
Bufw.write ("!hello world!");
Bufw.write ("!hello world!");
Use the method in the buffer to flush the data to the destination file.
Bufw.flush ();
Closes the buffer while closing the FW stream object
bufw.close ();
}
Second, BufferedReader class.
Construction Method: BufferedReader br = new Bufferreader (Reader in);
Main method: int read ();//Read single character.
int Read (char[] cbuf,int off,int len);//Read the character to a part of the array. Returns the number of characters read. Reach the tail, return-1.
String readLine (); Reads a line of text.
void Close (); Closes the stream. and releases all resources associated with the stream.
Package buffered;
Import Java.io.BufferedWriter;
Import Java.io.FileWriter;
Import java.io.IOException;
public class Bufferedwriterdemo {public
static void Main (string[] args) throws IOException {
FileWriter fw = new FileWriter ("Buffered.txt"); fw.write ("ok168"); fw.close ();
/**
* In order to improve the efficiency of writing, a character stream buffer is used.
* Creates a buffer object with a character write inflow and is associated with a stream object that specifies the buffer to be buffered.
*/
BufferedWriter BUFW = new BufferedWriter (FW);
Writes data to a buffer using a method in the buffer.
bufw.write ("Hello World!");
Bufw.newline ();
Bufw.newline ();
Bufw.write ("!hello world!");
Bufw.write ("!hello world!");
Use the method in the buffer to flush the data to the destination file.
Bufw.flush ();
Closes the buffer while closing the FW stream object
bufw.close ();
}
a custom Mybufferedreader class.
Package buffered;
Import Java.io.FileReader;
Import java.io.IOException;
public class Mybufferedreader {
private filereader fr;
Private char []BUF = new char[1024];
private int count = 0;
private int pos = 0;
Public Mybufferedreader (FileReader f) {
this.fr = f;
}
public int Myread () throws ioexception{
if (count = = 0) {
count = Fr.read (BUF);
pos = 0;
}
if (count<0)
return-1;
int ch = buf[pos++];
count--;
return ch;
}
Public String Myreadline () throws ioexception{
StringBuilder sb = new StringBuilder ();
int ch = 0;
while (ch = myread ())!=-1) {
if (ch = = ' \ r ')
continue;
if (ch = = ' \ n ') return
sb.tostring ();
Sb.append ((char) ch);
if (count = = 0) return
sb.tostring ();
}
return null;
}
public void Myclose () throws IOException {
fr.close ();
}
}
A small program that uses the BufferedReader and Bufferwriter methods to write a copy of text.
Package iotest;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
The public class Textcopybybuf {
/**
* First creates the files to be copied by the read character Data Flow Object Association.
* Create a Buffer object association stream object.
* Creates and writes characters from a buffer into a headlines file.
* @throws IOException
*
/public static void main (string[] args) throws IOException {
FileReader fr = New FileReader ("C:\\demo.txt");
FileWriter FW = new FileWriter ("D:\\love.txt");
BufferedReader bufr = new BufferedReader (FR);
BufferedWriter BUFW = new BufferedWriter (FW);
Write on one line.
String line = null;
while (line = Bufr.readline ())!= null) {
bufw.write (line);
Bufw.newline ();
Bufw.flush ();
}
/* A word is written in one word.
int ch = 0;
while (ch = bufr.read ())!=-1) {
bufw.write (ch);
} * *
bufr.close ();
Bufw.close ();
}