Now that the calculation has been done Java development for two years, back to think it is really not easy, Java things are more complex but if the basic work of the ability to upgrade quickly, this special finishing a bit about the file operation of the common code and everyone to share
1. Read the file (normal mode)
(1) The first method
[Java]View PlainCopy
- File f=new file ("D:" +file.separator+"test.txt");
- InputStream in=New FileInputStream (f);
- Byte[] b=new byte[(int) f.length ()];
- int len=0;
- int temp=0;
- while ((Temp=in.read ())!=-1) {
- b[len]= (byte) temp;
- len++;
- }
- System.out.println (new String (b,0,len,"GBK"));
- In.close ();
This method seems to use a little more
(2) The second method
[Java]View PlainCopy
- file f=new file ( "D:" +file.separator+
- inputstream in=new fileinputstream (f);
- byte[] b=new byte[ 1024];  
- int len =0;  
- while ((len= In.read (b))!=-1) {
- System.out.println (new string (b,0,len, " GBK ")
-  }  
- in.close ();
2. File read (memory mapped mode)
[Java]View PlainCopy
- File f=new file ("D:" +file.separator+"test.txt");
- FileInputStream in=New FileInputStream (f);
- FileChannel Chan=in.getchannel ();
- Mappedbytebuffer Buf=chan.map (FileChannel.MapMode.READ_ONLY, 0, F.length ());
- Byte[] b=new byte[(int) f.length ()];
- int len=0;
- while (Buf.hasremaining ()) {
- B[len]=buf.get ();
- len++;
- }
- Chan.close ();
- In.close ();
- System.out.println (new String (b,0,len,"GBK"));
The efficiency of this method is the best, the speed is also the fastest, because the program is directly operating the memory
3. File copy (side-read-write) operation
(1) The more common way
[Java]View PlainCopy
- Package org.lxh;
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import Java.io.FileOutputStream;
- Import Java.io.InputStream;
- Import Java.io.OutputStream;
- Public class ReadAndWrite {
- public static void Main (string[] args) throws Exception {
- File f=new file ("D:" +file.separator+"test.txt");
- InputStream in=New FileInputStream (f);
- OutputStream out=New FileOutputStream ("e:" +file.separator+"test.txt");
- int temp=0;
- While ((Temp=in.read ())!=-1) {
- Out.write (temp);
- }
- Out.close ();
- In.close ();
- }
- }
(2) Implementation of using memory mapping
[Java]View PlainCopy
- Package org.lxh;
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import Java.io.FileOutputStream;
- Import Java.nio.ByteBuffer;
- Import Java.nio.channels.FileChannel;
- Public class ReadAndWrite2 {
- public static void Main (string[] args) throws Exception {
- File f=new file ("D:" +file.separator+"test.txt");
- FileInputStream in=New FileInputStream (f);
- FileOutputStream out=New FileOutputStream ("e:" +file.separator+"test.txt");
- FileChannel Fin=in.getchannel ();
- FileChannel Fout=out.getchannel ();
- //Open buffer
- Bytebuffer buf=bytebuffer.allocate (1024);
- While ((Fin.read (BUF))!=-1) {
- //Reset buffer
- Buf.flip ();
- //Output buffers
- Fout.write (BUF);
- //Clear buffer
- Buf.clear ();
- }
- Fin.close ();
- Fout.close ();
- In.close ();
- Out.close ();
- }
- }
Common Java file read and write operations