The basic usage of bufferedwriter and BufferedReader, attached to the demo program. and a program that copies text files.

Source: Internet
Author: User
Tags readline

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 ();
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.