Java文件读写IO/NIO及性能比较总结

干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记。经常想得捡起,但总是因为一些原因,不能如愿。

其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来。

文件读写是一个在项目中经常遇到的工作,有些时候是因为维护,有些时候是新功能开发。我们的任务总是很重,工作节奏很快,快到我们不能停下脚步去总结。

文件读写有以下几种常用的方法

1、字节读写(InputStream/OutputStream)

2、字符读取(FileReader/FileWriter)

3、行读取(BufferedReader/BufferedWriter)

代码(以读取为例):

  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. /** 
  8.  * <b>文件读取类</b><br /> 
  9.  * 1、按字节读取文件内容<br /> 
  10.  * 2、按字符读取文件内容<br /> 
  11.  * 3、按行读取文件内容<br /> 
  12.  * @author qin_xijuan 
  13.  * 
  14.  */  
  15. public class FileOperate {  
  16.       
  17.     private static final String FILE_PATH = "d:/work/the List of Beautiful Music.txt";  
  18.   
  19.     /** 
  20.      * 以字节为单位读取文件内容 
  21.      * @param filePath:需要读取的文件路径 
  22.      */  
  23.     public static void readFileByByte(String filePath) {  
  24.         File file = new File(filePath);  
  25.         // InputStream:此抽象类是表示字节输入流的所有类的超类。  
  26.         InputStream ins = null ;  
  27.         try{  
  28.             // FileInputStream:从文件系统中的某个文件中获得输入字节。  
  29.             ins = new FileInputStream(file);  
  30.             int temp ;  
  31.             // read():从输入流中读取数据的下一个字节。  
  32.             while((temp = ins.read())!=-1){  
  33.                 System.out.write(temp);  
  34.             }  
  35.         }catch(Exception e){  
  36.             e.getStackTrace();  
  37.         }finally{  
  38.             if (ins != null){  
  39.                 try{  
  40.                     ins.close();  
  41.                 }catch(IOException e){  
  42.                     e.getStackTrace();  
  43.                 }  
  44.             }  
  45.         }  
  46.     }  
  47.       
  48.     /** 
  49.      * 以字符为单位读取文件内容 
  50.      * @param filePath 
  51.      */  
  52.     public static void readFileByCharacter(String filePath){  
  53.         File file = new File(filePath);  
  54.         // FileReader:用来读取字符文件的便捷类。  
  55.         FileReader reader = null;  
  56.         try{  
  57.             reader = new FileReader(file);  
  58.             int temp ;  
  59.             while((temp = reader.read()) != -1){  
  60.                 if (((char) temp) != '\r') {  
  61.                     System.out.print((char) temp);  
  62.                 }  
  63.             }  
  64.         }catch(IOException e){  
  65.             e.getStackTrace();  
  66.         }finally{  
  67.             if (reader != null){  
  68.                 try {  
  69.                     reader.close();  
  70.                 } catch (IOException e) {  
  71.                     e.printStackTrace();  
  72.                 }  
  73.             }  
  74.         }  
  75.     }  
  76.       
  77.     /** 
  78.      * 以行为单位读取文件内容 
  79.      * @param filePath 
  80.      */  
  81.     public static void readFileByLine(String filePath){  
  82.         File file = new File(filePath);  
  83.         // BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。  
  84.         BufferedReader buf = null;  
  85.         try{  
  86.             // FileReader:用来读取字符文件的便捷类。  
  87.             buf = new BufferedReader(new FileReader(file));  
  88.             // buf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));  
  89.             String temp = null ;  
  90.             while ((temp = buf.readLine()) != null ){  
  91.                 System.out.println(temp);  
  92.             }  
  93.         }catch(Exception e){  
  94.             e.getStackTrace();  
  95.         }finally{  
  96.             if(buf != null){  
  97.                 try{  
  98.                     buf.close();  
  99.                 } catch (IOException e) {  
  100.                     e.getStackTrace();  
  101.                 }  
  102.             }  
  103.         }  
  104.     }  
  105.   
  106.     public static void main(String args[]) {  
  107.         readFileByByte(FILE_PATH);  
  108.         readFileByCharacter(FILE_PATH);  
  109.         readFileByLine(FILE_PATH);  
  110.     }  
  111. }  
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
/**
 * <b>文件读取类</b><br />
 * 1、按字节读取文件内容<br />
 * 2、按字符读取文件内容<br />
 * 3、按行读取文件内容<br />
 * @author qin_xijuan
 *
 */
public class FileOperate {
    
    private static final String FILE_PATH = "d:/work/the List of Beautiful Music.txt";

    /**
     * 以字节为单位读取文件内容
     * @param filePath:需要读取的文件路径
     */
    public static void readFileByByte(String filePath) {
        File file = new File(filePath);
        // InputStream:此抽象类是表示字节输入流的所有类的超类。
        InputStream ins = null ;
        try{
            // FileInputStream:从文件系统中的某个文件中获得输入字节。
            ins = new FileInputStream(file);
            int temp ;
            // read():从输入流中读取数据的下一个字节。
            while((temp = ins.read())!=-1){
                System.out.write(temp);
            }
        }catch(Exception e){
            e.getStackTrace();
        }finally{
            if (ins != null){
                try{
                    ins.close();
                }catch(IOException e){
                    e.getStackTrace();
                }
            }
        }
    }
    
    /**
     * 以字符为单位读取文件内容
     * @param filePath
     */
    public static void readFileByCharacter(String filePath){
        File file = new File(filePath);
        // FileReader:用来读取字符文件的便捷类。
        FileReader reader = null;
        try{
            reader = new FileReader(file);
            int temp ;
            while((temp = reader.read()) != -1){
                if (((char) temp) != '\r') {
                    System.out.print((char) temp);
                }
            }
        }catch(IOException e){
            e.getStackTrace();
        }finally{
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 以行为单位读取文件内容
     * @param filePath
     */
    public static void readFileByLine(String filePath){
        File file = new File(filePath);
        // BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
        BufferedReader buf = null;
        try{
            // FileReader:用来读取字符文件的便捷类。
            buf = new BufferedReader(new FileReader(file));
            // buf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String temp = null ;
            while ((temp = buf.readLine()) != null ){
                System.out.println(temp);
            }
        }catch(Exception e){
            e.getStackTrace();
        }finally{
            if(buf != null){
                try{
                    buf.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }

    public static void main(String args[]) {
        readFileByByte(FILE_PATH);
        readFileByCharacter(FILE_PATH);
        readFileByLine(FILE_PATH);
    }
}
// ----------------------------------------------------------------- 分割线 -----------------------------------------------------------------------------

再经过两位同行的提点下,我对之前写的文件做了点修改,并通过读写一个1.2M的文本文件来测试各方法的性能。从多次测试结果来看,行读写却是是Java.nio更有效率。

经过修改之后的代码如下:

  1. package com.waddell.basic;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.FileReader;  
  9. import java.io.FileWriter;  
  10. import java.io.IOException;  
  11. import java.io.InputStream;  
  12. import java.io.OutputStream;  
  13. import java.nio.ByteBuffer;  
  14. import java.nio.channels.FileChannel;  
  15.   
  16. /** 
  17.  * <b>文件读取类</b><br /> 
  18.  * 1、按字节读取文件内容<br /> 
  19.  * 2、按字符读取文件内容<br /> 
  20.  * 3、按行读取文件内容<br /> 
  21.  *  
  22.  * @author qin_xijuan 
  23.  *  
  24.  */  
  25. public class FileOperate {  
  26.   
  27.     private static final String FILE_PATH = "d:/work/jipinwodi.txt";  
  28.   
  29.     /** 
  30.      * 以字节为单位读写文件内容 
  31.      *  
  32.      * @param filePath 
  33.      *            :需要读取的文件路径 
  34.      */  
  35.     public static void readFileByByte(String filePath) {  
  36.         File file = new File(filePath);  
  37.         // InputStream:此抽象类是表示字节输入流的所有类的超类。  
  38.         InputStream ins = null;  
  39.         OutputStream outs = null;  
  40.         try {  
  41.             // FileInputStream:从文件系统中的某个文件中获得输入字节。  
  42.             ins = new FileInputStream(file);  
  43.             outs = new FileOutputStream("d:/work/readFileByByte.txt");  
  44.             int temp;  
  45.             // read():从输入流中读取数据的下一个字节。  
  46.             while ((temp = ins.read()) != -1) {  
  47.                 outs.write(temp);  
  48.             }  
  49.         } catch (Exception e) {  
  50.             e.getStackTrace();  
  51.         } finally {  
  52.             if (ins != null && outs != null) {  
  53.                 try {  
  54.                     outs.close();  
  55.                     ins.close();  
  56.                 } catch (IOException e) {  
  57.                     e.getStackTrace();  
  58.                 }  
  59.             }  
  60.         }  
  61.     }  
  62.   
  63.     /** 
  64.      * 以字符为单位读写文件内容 
  65.      *  
  66.      * @param filePath 
  67.      */  
  68.     public static void readFileByCharacter(String filePath) {  
  69.         File file = new File(filePath);  
  70.         // FileReader:用来读取字符文件的便捷类。  
  71.         FileReader reader = null;  
  72.         FileWriter writer = null;  
  73.         try {  
  74.             reader = new FileReader(file);  
  75.             writer = new FileWriter("d:/work/readFileByCharacter.txt");  
  76.             int temp;  
  77.             while ((temp = reader.read()) != -1) {  
  78.                 writer.write((char)temp);  
  79.             }  
  80.         } catch (IOException e) {  
  81.             e.getStackTrace();  
  82.         } finally {  
  83.             if (reader != null && writer != null) {  
  84.                 try {  
  85.                     reader.close();  
  86.                     writer.close();  
  87.                 } catch (IOException e) {  
  88.                     e.printStackTrace();  
  89.                 }  
  90.             }  
  91.         }  
  92.     }  
  93.   
  94.     /** 
  95.      * 以行为单位读写文件内容 
  96.      *  
  97.      * @param filePath 
  98.      */  
  99.     public static void readFileByLine(String filePath) {  
  100.         File file = new File(filePath);  
  101.         // BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。  
  102.         BufferedReader bufReader = null;  
  103.         BufferedWriter bufWriter = null;  
  104.         try {  
  105.             // FileReader:用来读取字符文件的便捷类。  
  106.             bufReader = new BufferedReader(new FileReader(file));  
  107.             bufWriter = new BufferedWriter(new FileWriter("d:/work/readFileByLine.txt"));  
  108.             // buf = new BufferedReader(new InputStreamReader(new  
  109.             // FileInputStream(file)));  
  110.             String temp = null;  
  111.             while ((temp = bufReader.readLine()) != null) {  
  112.                 bufWriter.write(temp+"\n");  
  113.             }  
  114.         } catch (Exception e) {  
  115.             e.getStackTrace();  
  116.         } finally {  
  117.             if (bufReader != null && bufWriter != null) {  
  118.                 try {  
  119.                     bufReader.close();  
  120.                     bufWriter.close();  
  121.                 } catch (IOException e) {  
  122.                     e.getStackTrace();  
  123.                 }  
  124.             }  
  125.         }  
  126.     }  
  127.   
  128.     /** 
  129.      * 使用Java.nio ByteBuffer字节将一个文件输出至另一文件 
  130.      *  
  131.      * @param filePath 
  132.      */  
  133.     public static void readFileByBybeBuffer(String filePath) {  
  134.         FileInputStream in = null;  
  135.         FileOutputStream out = null;  
  136.         try {  
  137.             // 获取源文件和目标文件的输入输出流    
  138.             in = new FileInputStream(filePath);  
  139.             out = new FileOutputStream("d:/work/readFileByBybeBuffer.txt");  
  140.             // 获取输入输出通道  
  141.             FileChannel fcIn = in.getChannel();  
  142.             FileChannel fcOut = out.getChannel();  
  143.             ByteBuffer buffer = ByteBuffer.allocate(1024);  
  144.             while (true) {  
  145.                 // clear方法重设缓冲区,使它可以接受读入的数据  
  146.                 buffer.clear();  
  147.                 // 从输入通道中将数据读到缓冲区  
  148.                 int r = fcIn.read(buffer);  
  149.                 if (r == -1) {  
  150.                     break;  
  151.                 }  
  152.                 // flip方法让缓冲区可以将新读入的数据写入另一个通道    
  153.                 buffer.flip();  
  154.                 fcOut.write(buffer);  
  155.             }  
  156.   
  157.         } catch (Exception e) {  
  158.             e.printStackTrace();  
  159.         } finally {  
  160.             if (in != null && out != null) {  
  161.                 try {  
  162.                     in.close();  
  163.                     out.close();  
  164.                 } catch (IOException e) {  
  165.                     e.printStackTrace();  
  166.                 }  
  167.             }  
  168.         }  
  169.     }  
  170.       
  171.     public static long getTime(){  
  172.         return System.currentTimeMillis();  
  173.     }  
  174.   
  175.     public static void main(String args[]) {  
  176.         long time1 = getTime() ;  
  177.         // readFileByByte(FILE_PATH);// 8734,8281,8000,7781,8047  
  178.         // readFileByCharacter(FILE_PATH);// 734, 437, 437, 438, 422  
  179.         // readFileByLine(FILE_PATH);// 110, 94,  94,  110, 93  
  180.         readFileByBybeBuffer(FILE_PATH);// 125, 78,  62,  78, 62  
  181.         long time2 = getTime() ;  
  182.         System.out.println(time2-time1);  
  183.     }  
  184. }  
package com.waddell.basic;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <b>文件读取类</b><br />
 * 1、按字节读取文件内容<br />
 * 2、按字符读取文件内容<br />
 * 3、按行读取文件内容<br />
 * 
 * @author qin_xijuan
 * 
 */
public class FileOperate {

    private static final String FILE_PATH = "d:/work/jipinwodi.txt";

    /**
     * 以字节为单位读写文件内容
     * 
     * @param filePath
     *            :需要读取的文件路径
     */
    public static void readFileByByte(String filePath) {
        File file = new File(filePath);
        // InputStream:此抽象类是表示字节输入流的所有类的超类。
        InputStream ins = null;
        OutputStream outs = null;
        try {
            // FileInputStream:从文件系统中的某个文件中获得输入字节。
            ins = new FileInputStream(file);
            outs = new FileOutputStream("d:/work/readFileByByte.txt");
            int temp;
            // read():从输入流中读取数据的下一个字节。
            while ((temp = ins.read()) != -1) {
                outs.write(temp);
            }
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            if (ins != null && outs != null) {
                try {
                    outs.close();
                    ins.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }

    /**
     * 以字符为单位读写文件内容
     * 
     * @param filePath
     */
    public static void readFileByCharacter(String filePath) {
        File file = new File(filePath);
        // FileReader:用来读取字符文件的便捷类。
        FileReader reader = null;
        FileWriter writer = null;
        try {
            reader = new FileReader(file);
            writer = new FileWriter("d:/work/readFileByCharacter.txt");
            int temp;
            while ((temp = reader.read()) != -1) {
                writer.write((char)temp);
            }
        } catch (IOException e) {
            e.getStackTrace();
        } finally {
            if (reader != null && writer != null) {
                try {
                    reader.close();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 以行为单位读写文件内容
     * 
     * @param filePath
     */
    public static void readFileByLine(String filePath) {
        File file = new File(filePath);
        // BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
        BufferedReader bufReader = null;
        BufferedWriter bufWriter = null;
        try {
            // FileReader:用来读取字符文件的便捷类。
            bufReader = new BufferedReader(new FileReader(file));
            bufWriter = new BufferedWriter(new FileWriter("d:/work/readFileByLine.txt"));
            // buf = new BufferedReader(new InputStreamReader(new
            // FileInputStream(file)));
            String temp = null;
            while ((temp = bufReader.readLine()) != null) {
                bufWriter.write(temp+"\n");
            }
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            if (bufReader != null && bufWriter != null) {
                try {
                    bufReader.close();
                    bufWriter.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }

    /**
     * 使用Java.nio ByteBuffer字节将一个文件输出至另一文件
     * 
     * @param filePath
     */
    public static void readFileByBybeBuffer(String filePath) {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            // 获取源文件和目标文件的输入输出流  
            in = new FileInputStream(filePath);
            out = new FileOutputStream("d:/work/readFileByBybeBuffer.txt");
            // 获取输入输出通道
            FileChannel fcIn = in.getChannel();
            FileChannel fcOut = out.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (true) {
                // clear方法重设缓冲区,使它可以接受读入的数据
                buffer.clear();
                // 从输入通道中将数据读到缓冲区
                int r = fcIn.read(buffer);
                if (r == -1) {
                    break;
                }
                // flip方法让缓冲区可以将新读入的数据写入另一个通道  
                buffer.flip();
                fcOut.write(buffer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null && out != null) {
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static long getTime(){
        return System.currentTimeMillis();
    }

    public static void main(String args[]) {
        long time1 = getTime() ;
        // readFileByByte(FILE_PATH);// 8734,8281,8000,7781,8047
        // readFileByCharacter(FILE_PATH);// 734, 437, 437, 438, 422
        // readFileByLine(FILE_PATH);// 110, 94,  94,  110, 93
        readFileByBybeBuffer(FILE_PATH);// 125, 78,  62,  78, 62
        long time2 = getTime() ;
        System.out.println(time2-time1);
    }
}

在main方法中,调用各方法之后,有五组数据,分辨是我5次读写文件测试出来的时间(毫秒)。

关于Java.nio 请参考:http://www.iteye.com/topic/834447

付我个人测试:

  1.     public static void main(String args[]) {  
  2.         long time1 = getTime() ;  
  3. //         readFileByByte(FILE_PATH);     //2338,2286  
  4. //         readFileByCharacter(FILE_PATH);//160,162,158  
  5. //         readFileByLine(FILE_PATH);     //46,51,57  
  6. //        readFileByBybeBuffer(FILE_PATH);//19,18,17  
  7. //        readFileByBybeBuffer(FILE_PATH);//2048: 11,13  
  8. //        readFileByBybeBuffer(FILE_PATH);//1024*100 100k,711k: 6,6  
  9. //        readFileByBybeBuffer(FILE_PATH);//1024*100 100k,1422k: 7  
  10. //        readFileByBybeBuffer(FILE_PATH);//1024*100 100k,9951k: 49,48  
  11. //        readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,711k: 7,7  
  12. //        readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,1422k: 7,8  
  13. //        readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,9951k: 48,49  
  14. //        readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,711k: 21,13,17  
  15. //        readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,1422k: 16,17,14,15  
  16. //        readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,9951k:64,60  
  17.           
  18.         long time2 = getTime() ;  
  19.         System.out.println(time2-time1);  
  20.     }  
    public static void main(String args[]) {
        long time1 = getTime() ;
//         readFileByByte(FILE_PATH);     //2338,2286
//         readFileByCharacter(FILE_PATH);//160,162,158
//         readFileByLine(FILE_PATH);     //46,51,57
//        readFileByBybeBuffer(FILE_PATH);//19,18,17
//        readFileByBybeBuffer(FILE_PATH);//2048: 11,13
//        readFileByBybeBuffer(FILE_PATH);//1024*100 100k,711k: 6,6
//        readFileByBybeBuffer(FILE_PATH);//1024*100 100k,1422k: 7
//        readFileByBybeBuffer(FILE_PATH);//1024*100 100k,9951k: 49,48
//        readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,711k: 7,7
//        readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,1422k: 7,8
//        readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,9951k: 48,49
//        readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,711k: 21,13,17
//        readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,1422k: 16,17,14,15
//        readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,9951k:64,60
        
        long time2 = getTime() ;
        System.out.println(time2-time1);
    }


转自: http://www.cnblogs.com/waddell/archive/2013/01/24/2874104.html

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值