I 've been reading Java Io for the past few days and it's messy. Because it is very important, it is a bit confusing to read it over and over again.
The test code written last night was only filereader. I had nothing to do with the computer class. I modified the code and added filewriter, bufferedreader, and bufferedwriter.
For example, this article does not want to explain how to use the above class. It is very important to explain how to use the flush () method!
At the beginning, a love.txt file was created under the root directory of drive D, which is very small and contains only a few characters. The flush(upload folder is not used at the beginning. The generated android.txt file is empty, and the expected file is the copy of the love.txt file. I read this code repeatedly. Is it true that filewriter or bufferedwriter is useless? I found this part is correct. Then let's look at the API. We can see the flush () method, and then we suddenly realized it. I forgot this important thing. Before closing the buffer zone, I should forcibly write the "residual" data in the buffer zone. Later, I increased the volume of love.txtto 190 2k2. I did not call flush((, then the android.txt file output only KB. Obviously, data is lost, and the loss is the "residual" data in the buffer zone. The output android.txt file is the same size as the love.txt file, and the data is not lost.
The starting reason is that the love.txt file contains too little content and is not filled with a buffer zone. It is regarded as "residual" data, so it has not been copied. Later, we increased the love.txt file and found that the data was lost...
The flush () method is described as follows:
Public void flush ()
Throws ioexception refreshes the buffer of the stream.
I personally feel that the word "refresh" is not appropriate. It is more appropriate to use "Force write buffer data.
The usage of a small flush () is so big that I can remember it and never forget it.
Hungry, eat with a good mood...
Import Java. io. *; <br/> public class readerandbufferedtest {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> file filedir = new file ("D:/AAA/BBB/PPLs"); <br/> filedir. mkdirs (); <br/> file = new file (filedir, "android.txt"); </P> <p> filereader in = new filereader ("D: /love.txt "); </P> <p> filewriter out = new filewriter (File); <br/> bufferedreader BR = new bufferedreader (in ); <br/> bufferedwriter BW = new B Ufferedwriter (out); </P> <p> int B; <br/> system. out. println ("read files below"); <br/> while (B = BR. read ())! =-1) {<br/> system. out. print (char) B); <br/> BW. write (B); <br/>}< br/> BW. flush (); // be sure to write <br/> in. close (); // do not write BR. close () and BW. close () <br/> out. close (); </P> <p >}catch (filenotfoundexception e) {<br/> system. out. println ("/n file not found"); <br/> system. exit (-1); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/> system. exit (-1); <br/>}< br/> system. out. println ("/n file copied"); <br/>}< br/>}