java常用的文件读写操作

现在算算已经做java开发两年了,回过头想想还真是挺不容易的,java的东西是比较复杂但是如果基础功扎实的话能力的提升就很快,这次特别整理了点有关文件操作的常用代码和大家分享

1.文件的读取(普通方式)

(1)第一种方法

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. File f=new File("d:"+File.separator+"test.txt");  
  2. InputStream in=new FileInputStream(f);  
  3. byte[] b=new byte[(int)f.length()];  
  4. int len=0;  
  5. int temp=0;  
  6. while((temp=in.read())!=-1){  
  7.        b[len]=(byte)temp;  
  8.        len++;  
  9. }  
  10. System.out.println(new String(b,0,len,"GBK"));  
  11. in.close();  


这种方法貌似用的比较多一点

(2)第二种方法

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. File f=new File("d:"+File.separator+"test.txt");  
  2. InputStream in=new FileInputStream(f);  
  3. byte[] b=new byte[1024];  
  4. int len=0;  
  5. while((len=in.read(b))!=-1){  
  6.     System.out.println(new String(b,0,len,"GBK"));  
  7.  }  
  8. in.close();  


2.文件读取(内存映射方式)

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. File f=new File("d:"+File.separator+"test.txt");  
  2. FileInputStream in=new FileInputStream(f);  
  3. FileChannel chan=in.getChannel();  
  4. MappedByteBuffer buf=chan.map(FileChannel.MapMode.READ_ONLY, 0, f.length());  
  5. byte[] b=new byte[(int)f.length()];  
  6. int len=0;  
  7. while(buf.hasRemaining()){  
  8.    b[len]=buf.get();  
  9.     len++;  
  10.  }  
  11. chan.close();  
  12. in.close();  
  13. System.out.println(new String(b,0,len,"GBK"));  

这种方式的效率是最好的,速度也是最快的,因为程序直接操作的是内存

3.文件复制(边读边写)操作

(1)比较常用的方式

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package org.lxh;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.   
  9. public class ReadAndWrite {  
  10.   
  11.     public static void main(String[] args) throws Exception {  
  12.         File f=new File("d:"+File.separator+"test.txt");  
  13.         InputStream in=new FileInputStream(f);  
  14.         OutputStream out=new FileOutputStream("e:"+File.separator+"test.txt");  
  15.         int temp=0;  
  16.         while((temp=in.read())!=-1){  
  17.             out.write(temp);  
  18.         }  
  19.         out.close();  
  20.         in.close();  
  21.     }  
  22.   
  23. }  


(2)使用内存映射的实现

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. package org.lxh;  
    2.   
    3. import java.io.File;  
    4. import java.io.FileInputStream;  
    5. import java.io.FileOutputStream;  
    6. import java.nio.ByteBuffer;  
    7. import java.nio.channels.FileChannel;  
    8. public class ReadAndWrite2 {  
    9.   
    10.    public static void main(String[] args) throws Exception {  
    11.     File f=new File("d:"+File.separator+"test.txt");  
    12.     FileInputStream in=new FileInputStream(f);  
    13.     FileOutputStream out=new FileOutputStream("e:"+File.separator+"test.txt");  
    14.         FileChannel fin=in.getChannel();  
    15.         FileChannel fout=out.getChannel();  
    16.         //开辟缓冲  
    17.         ByteBuffer buf=ByteBuffer.allocate(1024);  
    18.         while((fin.read(buf))!=-1){  
    19.             //重设缓冲区  
    20.             buf.flip();  
    21.             //输出缓冲区  
    22.             fout.write(buf);  
    23.             //清空缓冲区  
    24.             buf.clear();  
    25.         }  
    26.         fin.close();  
    27.         fout.close();  
    28.         in.close();  
    29.         out.close();  
    30.     }  
    31.   
    32. }  
原文地址:https://www.cnblogs.com/liuchaogege/p/5833405.html