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

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

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

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

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

1、字节读写(InputStream/OutputStream)

2、字符读取(FileReader/FileWriter)

3、行读取(BufferedReader/BufferedWriter)

代码(以读取为例):

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值