Java Tour (25)--file copy, character stream buffer, Bufferedwriter,bufferedreader, copy file via buffer, readline working principle, custom ReadLine

Source: Internet
Author: User
Tags file copy

Java Tour (25)--file copy, character stream buffer, Bufferedwriter,bufferedreader, copy file via buffer, readline working principle, custom ReadLine

Let's continue IO for the last space

I. Text copying

Read and write all said, we look at the other operations, we first look at the replication

    • The principle of replication: In fact, the file data under the C drive is stored in a file in D.

Steps to implement:
1. Create a file in the D drive to store the data in the file
2. Define read stream and file association
3. Storage of data through continuous reading and writing
Close Resource

 PackageCom.lgl.hellojava;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException; Public  class Hellojjava {     Public Static void Main(string[] args)        {copy_1 ();    Copy_2 (); }//Read a character from the C drive and write a character to the D drive     Public Static void copy_1() {Try{//Create a destinationFileWriter FW =NewFileWriter ("Copy_1.txt");//associated with an existing fileFileReader FR =NewFileReader ("Copy_1.txt");intCH =0; while(ch = fr.read ())! =-1) {//read one to write aFw.write (CH);            } fw.close ();        Fr.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }    } Public Static void copy_2() {FileWriter FW =NULL; FileReader FR =NULL;Try{FW =NewFileWriter ("Copy_2.txt"); FR =NewFileReader ("Copy_2.txt");Char[] buf =New Char[1024x768];intLen =0; while(len = Fr.read (BUF))! =-1) {Fw.write (buf,0, Len); }        }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }finally{if(FR! =NULL) {Try{Fr.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }            }if(FW! =NULL) {Try{Fw.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }            }        }    }}

Here are two ways to copy the way, in fact, are sorting out a good idea, read and write a process!

Two. Buffer of character streams

Character stream buffer, which improves the reading and writing efficiency of the data, he has two sub-classes


    • BufferedWriter
    • BufferedReader

The
buffer must be combined with willow before it can be used
The function of convection is enhanced
on the basis of flow.
1.BufferedWriter

The presence of a buffer is a result of increasing the efficiency of the stream, so there must be a stream object before the buffer is created, so let's look at the example

 PackageCom.lgl.hellojava;ImportJava.io.BufferedWriter;ImportJava.io.FileWriter;ImportJava.io.IOException; Public  class Hellojjava {     Public Static void Main(string[] args) {Try{//Create a character to write to the stream objectFileWriter FW =NewFileWriter ("Buffer.txt");//In order to improve the efficiency of writing stream added buffer technologyBufferedWriter BUFW =NewBufferedWriter (FW);//Write DataBufw.write ("Hello");//Line breakBufw.newline ();//As long as the buffer is used, it needs to be refreshedBufw.flush ();//Buffer closed is the associated streamBufw.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }    }}

Use is the basis of comparison, we can also see

2.BufferedReader

Efficient reading

We look directly at the code

 PackageCom.lgl.hellojava;ImportJava.io.BufferedReader;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.IOException; Public  class Hellojjava {     Public Static void Main(string[] args) {Try{//Create a read stream object and file associatedFileReader FR =NewFileReader ("Buffer.txt");//In order to improve efficiency, add buffer technologyBufferedReader BFR =NewBufferedReader (FR); String line =NULL; while(line = Bfr.readline ())! =NULL) {System.out.println (line);        } bfr.close (); }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }    }}

So we can all come out.

Three. Copying files through a buffer

OK, we still copy the file this question, now we have buffer, how do we copy the file?

 PackageCom.lgl.hellojava;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException; Public  class Hellojjava {     Public Static void Main(string[] args) {/** * Buffer file copy */BufferedReader BUFR =NULL; BufferedWriter BUFW =NULL;Try{BUFR =NewBufferedReader (NewFileReader ("Buffer.txt")); BUFW =NewBufferedWriter (NewFileWriter ("Buffercopy.txt")); String line =NULL; while(line = Bufr.readline ())! =NULL) {Bufw.write (line); }//Close streamBufr.close ();        Bufw.close (); }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }    }}

This way, you can copy the file.

Four. How ReadLine works

We notice that we are going to use this method ReadLine, whether reading a line or reading more than one character, is actually a read on the hard disk, so the end use of the Read method is a reading method

    • In fact, he has an array in memory, you read it and did not immediately read, but temporarily stored up, this is the buffer,

When I read a newline, I go back to a row of data, and that's how he works.

Five. Custom ReadLine

We understand how ReadLine works, so we can try to change him, how about a custom one? Let's try.

 PackageCom.lgl.hellojava;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.IOException; Public  class Hellojjava {     Public Static void Main(string[] args) {/** * Custom ReadLine * /FileReader fr;Try{FR =NewFileReader ("Buffer.txt"); Mybufferreader my =NewMybufferreader (FR); String line =NULL; while(line = My.myreadline ())! =NULL) {System.out.println (line); }        }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }}}class Mybufferreader {PrivateFileReader fr; Public Mybufferreader(FileReader fr) { This. fr = FR; }//A method to read one line at a time     PublicStringMyreadline()throwsIOException {//define temporary containersStringBuilder SB =NewStringBuilder ();intCH =0; while(ch = fr.read ())! =-1) {if(ch = =' \ r ') {Continue; }Else if(ch = =' \ n ') {returnSb.tostring (); }Else{Sb.append (Char) ch); }        }if(Sb.length ()! =0){returnSb.tostring (); }return NULL; } Public void Close()throwsIOException {fr.close (); }}

Take a closer look at the realization of ideas, quietly see, yes, we can also achieve, good, we this article to here is OK, is the end, we will continue to the next article Io, after all, this is a big knowledge point!

Interested in adding a group: 555974449

Java Tour (25)--file copy, character stream buffer, Bufferedwriter,bufferedreader, copy file via buffer, readline working principle, custom ReadLine

Related Article

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.