/*Key: "In" refers to the memory, "out" refers to the outside of memory*/ImportJava.io.*; Public classmyreadfile{ Public Static voidMain (string[] args) {Try{ /*The first step is to locate the data source && destination file*/File F=NewFile ("D:/lab_2/test.txt"); File F2=NewFile ("D:/lab_2/test2.txt"); /*Step Two, build the input && output pipeline*/FileInputStream fis=NewFileInputStream (f); FileOutputStream Fos=NewFileOutputStream (F2); /*step three, Operation Pipeline*/ for(; fis.available () > 0;) Fos.write (Fis.read ()); }Catch(Exception e) {} }}
Because it is a byte of one byte read and write, when the file is slightly larger (greater than 1MB), the speed is very slow.
You can read and write 1000 bytes at a time with a slight modification.
byte New byte [+]; for (; fis.available () > 0;) fis.read (TMP); Fos.write (TMP);
It is said that 2 of the n-th computer processing efficiency will be faster.
byte New byte [8192]; for (; fis.available () > 0;) fis.read (TMP); Fos.write (TMP);
Finally, the Standard Edition.
/*step three, Operation Pipeline*/ byte[] tmp =New byte[8192]; //handle most of the content intLength = fis.available ()/8192; for(inti = 0; i < length; i + +) {fis.read (TMP); Fos.write (TMP); } //processing the last remaining content intSize =Fis.read (TMP); Fos.write (TMP,0, size);
/*
The last sentence is changed to:
int left_length = fis.availble ();
Fis.read (TMP, 0, left_length);
Fos.write (TMP, 0, left_length);
There's no big problem.
*/
The simplest iostream--file copy