天天看點

Java檔案讀寫IO/NIO及性能比較總結

幹Java這麼久,一直在做WEB相關的項目,一些基礎類差不多都已經忘記。經常想得撿起,但總是因為一些原因,不能如願。

其實不是沒有時間,隻是有些時候疲于總結,今得空,下定決心将丢掉的都給撿起來。

檔案讀寫是一個在項目中經常遇到的工作,有些時候是因為維護,有些時候是新功能開發。我們的任務總是很重,工作節奏很快,快到我們不能停下腳步去總結。

檔案讀寫有以下幾種常用的方法

1、位元組讀寫(InputStream/OutputStream)

2、字元讀取(FileReader/FileWriter)

3、行讀取(BufferedReader/BufferedWriter)

代碼(以讀取為例):

[java] view plain copy print ?

Java檔案讀寫IO/NIO及性能比較總結
Java檔案讀寫IO/NIO及性能比較總結
  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. public class FileOperate {  
  8.     private static final String FILE_PATH = "d:/work/the List of Beautiful Music.txt";  
  9.     public static void readFileByByte(String filePath) {  
  10.         File file = new File(filePath);  
  11.         // InputStream:此抽象類是表示位元組輸入流的所有類的超類。  
  12.         InputStream ins = null ;  
  13.         try{  
  14.             // FileInputStream:從檔案系統中的某個檔案中獲得輸入位元組。  
  15.             ins = new FileInputStream(file);  
  16.             int temp ;  
  17.             // read():從輸入流中讀取資料的下一個位元組。  
  18.             while((temp = ins.read())!=-1){  
  19.                 System.out.write(temp);  
  20.             }  
  21.         }catch(Exception e){  
  22.             e.getStackTrace();  
  23.         }finally{  
  24.             if (ins != null){  
  25.                 try{  
  26.                     ins.close();  
  27.                 }catch(IOException e){  
  28.                     e.getStackTrace();  
  29.                 }  
  30.             }  
  31.         }  
  32.     }  
  33.     public static void readFileByCharacter(String filePath){  
  34.         File file = new File(filePath);  
  35.         // FileReader:用來讀取字元檔案的便捷類。  
  36.         FileReader reader = null;  
  37.         try{  
  38.             reader = new FileReader(file);  
  39.             int temp ;  
  40.             while((temp = reader.read()) != -1){  
  41.                 if (((char) temp) != '\r') {  
  42.                     System.out.print((char) temp);  
  43.                 }  
  44.             }  
  45.         }catch(IOException e){  
  46.             e.getStackTrace();  
  47.         }finally{  
  48.             if (reader != null){  
  49.                 try {  
  50.                     reader.close();  
  51.                 } catch (IOException e) {  
  52.                     e.printStackTrace();  
  53.                 }  
  54.             }  
  55.         }  
  56.     }  
  57.     public static void readFileByLine(String filePath){  
  58.         File file = new File(filePath);  
  59.         // BufferedReader:從字元輸入流中讀取文本,緩沖各個字元,進而實作字元、數組和行的高效讀取。  
  60.         BufferedReader buf = null;  
  61.         try{  
  62.             // FileReader:用來讀取字元檔案的便捷類。  
  63.             buf = new BufferedReader(new FileReader(file));  
  64.             // buf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));  
  65.             String temp = null ;  
  66.             while ((temp = buf.readLine()) != null ){  
  67.                 System.out.println(temp);  
  68.             }  
  69.         }catch(Exception e){  
  70.             e.getStackTrace();  
  71.         }finally{  
  72.             if(buf != null){  
  73.                 try{  
  74.                     buf.close();  
  75.                 } catch (IOException e) {  
  76.                     e.getStackTrace();  
  77.                 }  
  78.             }  
  79.         }  
  80.     }  
  81.     public static void main(String args[]) {  
  82.         readFileByByte(FILE_PATH);  
  83.         readFileByCharacter(FILE_PATH);  
  84.         readFileByLine(FILE_PATH);  
  85.     }  
  86. }  
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更有效率。

經過修改之後的代碼如下:

[java] view plain copy print ?

Java檔案讀寫IO/NIO及性能比較總結
Java檔案讀寫IO/NIO及性能比較總結
  1. package com.waddell.basic;  
  2. import java.io.BufferedReader;  
  3. import java.io.BufferedWriter;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.FileReader;  
  8. import java.io.FileWriter;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  11. import java.io.OutputStream;  
  12. import java.nio.ByteBuffer;  
  13. import java.nio.channels.FileChannel;  
  14. public class FileOperate {  
  15.     private static final String FILE_PATH = "d:/work/jipinwodi.txt";  
  16.     public static void readFileByByte(String filePath) {  
  17.         File file = new File(filePath);  
  18.         // InputStream:此抽象類是表示位元組輸入流的所有類的超類。  
  19.         InputStream ins = null;  
  20.         OutputStream outs = null;  
  21.         try {  
  22.             // FileInputStream:從檔案系統中的某個檔案中獲得輸入位元組。  
  23.             ins = new FileInputStream(file);  
  24.             outs = new FileOutputStream("d:/work/readFileByByte.txt");  
  25.             int temp;  
  26.             // read():從輸入流中讀取資料的下一個位元組。  
  27.             while ((temp = ins.read()) != -1) {  
  28.                 outs.write(temp);  
  29.             }  
  30.         } catch (Exception e) {  
  31.             e.getStackTrace();  
  32.         } finally {  
  33.             if (ins != null && outs != null) {  
  34.                 try {  
  35.                     outs.close();  
  36.                     ins.close();  
  37.                 } catch (IOException e) {  
  38.                     e.getStackTrace();  
  39.                 }  
  40.             }  
  41.         }  
  42.     }  
  43.     public static void readFileByCharacter(String filePath) {  
  44.         File file = new File(filePath);  
  45.         // FileReader:用來讀取字元檔案的便捷類。  
  46.         FileReader reader = null;  
  47.         FileWriter writer = null;  
  48.         try {  
  49.             reader = new FileReader(file);  
  50.             writer = new FileWriter("d:/work/readFileByCharacter.txt");  
  51.             int temp;  
  52.             while ((temp = reader.read()) != -1) {  
  53.                 writer.write((char)temp);  
  54.             }  
  55.         } catch (IOException e) {  
  56.             e.getStackTrace();  
  57.         } finally {  
  58.             if (reader != null && writer != null) {  
  59.                 try {  
  60.                     reader.close();  
  61.                     writer.close();  
  62.                 } catch (IOException e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.             }  
  66.         }  
  67.     }  
  68.     public static void readFileByLine(String filePath) {  
  69.         File file = new File(filePath);  
  70.         // BufferedReader:從字元輸入流中讀取文本,緩沖各個字元,進而實作字元、數組和行的高效讀取。  
  71.         BufferedReader bufReader = null;  
  72.         BufferedWriter bufWriter = null;  
  73.         try {  
  74.             // FileReader:用來讀取字元檔案的便捷類。  
  75.             bufReader = new BufferedReader(new FileReader(file));  
  76.             bufWriter = new BufferedWriter(new FileWriter("d:/work/readFileByLine.txt"));  
  77.             // buf = new BufferedReader(new InputStreamReader(new  
  78.             // FileInputStream(file)));  
  79.             String temp = null;  
  80.             while ((temp = bufReader.readLine()) != null) {  
  81.                 bufWriter.write(temp+"\n");  
  82.             }  
  83.         } catch (Exception e) {  
  84.             e.getStackTrace();  
  85.         } finally {  
  86.             if (bufReader != null && bufWriter != null) {  
  87.                 try {  
  88.                     bufReader.close();  
  89.                     bufWriter.close();  
  90.                 } catch (IOException e) {  
  91.                     e.getStackTrace();  
  92.                 }  
  93.             }  
  94.         }  
  95.     }  
  96.     public static void readFileByBybeBuffer(String filePath) {  
  97.         FileInputStream in = null;  
  98.         FileOutputStream out = null;  
  99.         try {  
  100.             // 擷取源檔案和目标檔案的輸入輸出流    
  101.             in = new FileInputStream(filePath);  
  102.             out = new FileOutputStream("d:/work/readFileByBybeBuffer.txt");  
  103.             // 擷取輸入輸出通道  
  104.             FileChannel fcIn = in.getChannel();  
  105.             FileChannel fcOut = out.getChannel();  
  106.             ByteBuffer buffer = ByteBuffer.allocate(1024);  
  107.             while (true) {  
  108.                 // clear方法重設緩沖區,使它可以接受讀入的資料  
  109.                 buffer.clear();  
  110.                 // 從輸入通道中将資料讀到緩沖區  
  111.                 int r = fcIn.read(buffer);  
  112.                 if (r == -1) {  
  113.                     break;  
  114.                 }  
  115.                 // flip方法讓緩沖區可以将新讀入的資料寫入另一個通道    
  116.                 buffer.flip();  
  117.                 fcOut.write(buffer);  
  118.             }  
  119.         } catch (Exception e) {  
  120.             e.printStackTrace();  
  121.         } finally {  
  122.             if (in != null && out != null) {  
  123.                 try {  
  124.                     in.close();  
  125.                     out.close();  
  126.                 } catch (IOException e) {  
  127.                     e.printStackTrace();  
  128.                 }  
  129.             }  
  130.         }  
  131.     }  
  132.     public static long getTime(){  
  133.         return System.currentTimeMillis();  
  134.     }  
  135.     public static void main(String args[]) {  
  136.         long time1 = getTime() ;  
  137.         // readFileByByte(FILE_PATH);// 8734,8281,8000,7781,8047  
  138.         // readFileByCharacter(FILE_PATH);// 734, 437, 437, 438, 422  
  139.         // readFileByLine(FILE_PATH);// 110, 94,  94,  110, 93  
  140.         readFileByBybeBuffer(FILE_PATH);// 125, 78,  62,  78, 62  
  141.         long time2 = getTime() ;  
  142.         System.out.println(time2-time1);  
  143.     }  
  144. }  
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

付我個人測試:

[java] view plain copy print ?

Java檔案讀寫IO/NIO及性能比較總結
Java檔案讀寫IO/NIO及性能比較總結
  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.         long time2 = getTime() ;  
  18.         System.out.println(time2-time1);  
  19.     }  
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

繼續閱讀