If we want to do a little bit of I/O, like copying files, I find a string of code on the Web:
1 ImportJava.io.*;2 Public classSS {3 Public Static voidMain (string[] args)throwsException {4File f =NewFile ("d:\\ large database file. mdf"); 5FileInputStream FIS =NewFileInputStream (f);6FileOutputStream fos =NewFileOutputStream ("e:\\" +f.getname ()); 7 intLength = 0;8 byte[] B =New byte[1024];9 while(length = Fis.read (b))! =-1)Ten { OneFos.write (b, 0, length); A } - fos.close (); - fis.close (); the } -}
This code constructs a FileInputStream object with the file object, which is used as a read-in memory, and a FileOutputStream object that is used as output to the hard disk.
Every time I read 1024 bytes from the D disk to the memory, and then write to the E disk, it seems that there is no problem.
However, the disk I/O is turned on every time you read 1024 bytes, which can be very time consuming.
If you use Bufferedinputstream, it will be much better.
Bufferedinputstream in the internal maintenance of a buffer, every time we read is in the read buffer, only buffer is not large enough to go to the read disk. This can greatly reduce the number of I/O, because from the hard disk read to buffer at a time more than a byte, then read in the future can read from the buffer directly without the drive.
The NIO portion continues later
Java Preliminary I/O understanding