Bufferinputstream, Bufferoutputstream, BufferedReader, BufferedWriter, Java code uses BufferedReader and bufferedwriter to make a copy of a text file

Source: Internet
Author: User

Features of Bufferinputstream and Bufferoutputstream:

Buffered bytes input and output stream, buffer stream is a processing stream, it does not directly connect to the data source/destination, but a byte stream as a parameter, on the basis of the node flow to provide some simple operation.

First of all, the principle of non-buffered flow, it reads to a byte/character, to the user-specified path to write out, read a write one, so slow, with the buffer stream working principle, read to a byte/character, first output, and so on to fill the maximum capacity of the buffer once written out, thereby improving the efficiency.

Advantages: Reduce the number of hard disk reads, reduce the loss of the hard disk.

Report:

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.Reader;
Import Java.io.Writer;

/**
* Use BufferedReader and bufferedwriter to make copies of text files
* @author Administrator
*
*/
public class Bufferoperatestr {
public static void Main (string[] args) {
Copy ();
}
static void Copy () {
Reader reader = null;
BufferedReader br = null;
writer writer = null;
BufferedWriter bw = NULL;
try {
Part I: Preparing to read data from a file to a program
reader = new FileReader (New File ("D://a.txt"));
Create a buffered stream wrapper object reader
br = new BufferedReader (reader);
Part two: Preparing to write to a file from a program; writing to an object writer
writer = new FileWriter (New File ("D:\\a3.txt"));
Creating a buffered stream wrapper writer
BW = new BufferedWriter (writer);
String str = NULL;
Read and write with circular edges
while ((Str=br.readline ())!=null) {
Bw.write (str);
Bw.newline ();
}
Bw.flush (); emptying buffers
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (bw!=null) {
Bw.close ();
}
if (br!=null) {
Br.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

Bufferinputstream, Bufferoutputstream, BufferedReader, BufferedWriter, Java code uses BufferedReader and bufferedwriter to make a copy of a text file

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.