zoukankan      html  css  js  c++  java
  • Java对文件的读取方式以及它们的优缺点

    Java常用的对文件的读取方式基本包括:

    • BufferedReader -> readLine(): 按行读取文件,直到读取内容==null
    • FileInputStream -> read() : 按字节读取文件,直到读取内容==-1
    • InputStreamReader -> read() : 按字符读取文件,直到读取内容==-1
    • RandomAccessFile -> seek() : 可以指定文件的读取位置,以字节为单位
    • Scanner -> nextLine() : 按行读取文件,直到hasNextLine() == false
      1 public class ReadFile {
      2     /**
      3      * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
      4      */
      5     public static void readFileByBytes(String fileName) {
      6         File file = new File(fileName);
      7         InputStream in = null;
      8         try {
      9             System.out.println("以字节为单位读取文件内容,一次读一个字节:");
     10             // 一次读一个字节
     11             in = new FileInputStream(file);
     12             int tempbyte;
     13             while ((tempbyte = in.read()) != -1) {
     14                 System.out.write(tempbyte);
     15             }
     16             in.close();
     17         } catch (IOException e) {
     18             e.printStackTrace();
     19             return;
     20         }
     21         try {
     22             System.out.println("以字节为单位读取文件内容,一次读多个字节:");
     23             // 一次读多个字节
     24             byte[] tempbytes = new byte[10];
     25             int byteread = 0;
     26             in = new FileInputStream(fileName);
     27             ReadFile.showAvailableBytes(in);
     28             // 读入多个字节到字节数组中,byteread为一次读入的字节数
     29             while ((byteread = in.read(tempbytes)) != -1) {
     30                 System.out.write(tempbytes, 0, byteread);
     31 //                System.out.println("上面读取了10个字节");
     32             }
     33         } catch (Exception e1) {
     34             e1.printStackTrace();
     35         } finally {
     36             if (in != null) {
     37                 try {
     38                     in.close();
     39                 } catch (IOException e1) {
     40                 }
     41             }
     42         }
     43     }
     44 
     45     /**
     46      * 以字符为单位读取文件,常用于读文本,数字等类型的文件
     47      */
     48     public static void readFileByChars(String fileName) {
     49         File file = new File(fileName);
     50         Reader reader = null;
     51         try {
     52             System.out.println("以字符为单位读取文件内容,一次读一个字节:");
     53             // 一次读一个字符
     54             reader = new InputStreamReader(new FileInputStream(file),"gbk");
     55             int tempchar;
     56             while ((tempchar = reader.read()) != -1) {
     57                 // 对于windows下,
    这两个字符在一起时,表示一个换行。
     58                 // 但如果这两个字符分开显示时,会换两次行。
     59                 // 因此,屏蔽掉
    ,或者屏蔽
    。否则,将会多出很多空行。
     60                 if (((char) tempchar) != '
    ') {
     61                     System.out.print((char) tempchar);
     62                 }
     63             }
     64             reader.close();
     65         } catch (Exception e) {
     66             e.printStackTrace();
     67         }
     68         try {
     69             System.out.println("以字符为单位读取文件内容,一次读多个字节:");
     70             // 一次读多个字符
     71             char[] tempchars = new char[30];
     72             int charread = 0;
     73             reader = new InputStreamReader(new FileInputStream(fileName));
     74             // 读入多个字符到字符数组中,charread为一次读取字符数
     75             while ((charread = reader.read(tempchars)) != -1) {
     76                 // 同样屏蔽掉
    不显示
     77                 if ((charread == tempchars.length)
     78                         && (tempchars[tempchars.length - 1] != '
    ')) {
     79                     System.out.print(tempchars);
     80                 } else {
     81                     for (int i = 0; i < charread; i++) {
     82                         if (tempchars[i] == '
    ') {
     83                             continue;
     84                         } else {
     85                             System.out.print(tempchars[i]);
     86                         }
     87                     }
     88                 }
     89             }
     90 
     91         } catch (Exception e1) {
     92             e1.printStackTrace();
     93         } finally {
     94             if (reader != null) {
     95                 try {
     96                     reader.close();
     97                 } catch (IOException e1) {
     98                 }
     99             }
    100         }
    101     }
    102 
    103     /**
    104      * 以行为单位读取文件,常用于读面向行的格式化文件
    105      */
    106     public static void readFileByLines(String fileName) {
    107         File file = new File(fileName);
    108         BufferedReader reader = null;
    109         try {
    110             System.out.println("以行为单位读取文件内容,一次读一整行:");
    111             reader = new BufferedReader(new FileReader(file));
    112             String tempString = null;
    113             int line = 1;
    114             // 一次读入一行,直到读入null为文件结束
    115             while ((tempString = reader.readLine()) != null) {
    116                 // 显示行号
    117                 System.out.println("line " + line + ": " + tempString);
    118                 line++;
    119             }
    120             reader.close();
    121         } catch (IOException e) {
    122             e.printStackTrace();
    123         } finally {
    124             if (reader != null) {
    125                 try {
    126                     reader.close();
    127                 } catch (IOException e1) {
    128                 }
    129             }
    130         }
    131     }
    132 
    133     /**
    134      * 随机读取文件内容
    135      */
    136     public static void readFileByRandomAccess(String fileName) {
    137         RandomAccessFile randomFile = null;
    138         try {
    139             System.out.println("随机读取一段文件内容:");
    140             // 打开一个随机访问文件流,按只读方式
    141             randomFile = new RandomAccessFile(fileName, "r");
    142             // 文件长度,字节数
    143             long fileLength = randomFile.length();
    144             // 读文件的起始位置
    145             int beginIndex = (fileLength > 4) ? 4 : 0;
    146             // 将读文件的开始位置移到beginIndex位置。
    147             randomFile.seek(beginIndex);
    148             byte[] bytes = new byte[10];
    149             int byteread = 0;
    150             // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
    151             // 将一次读取的字节数赋给byteread
    152             while ((byteread = randomFile.read(bytes)) != -1) {
    153                 System.out.write(bytes, 0, byteread);
    154             }
    155         } catch (IOException e) {
    156             e.printStackTrace();
    157         } finally {
    158             if (randomFile != null) {
    159                 try {
    160                     randomFile.close();
    161                 } catch (IOException e1) {
    162                 }
    163             }
    164         }
    165     }
    166 
    167     /**
    168      * 显示输入流中还剩的字节数
    169      */
    170     private static void showAvailableBytes(InputStream in) {
    171         try {
    172             System.out.println("当前字节输入流中的字节数为:" + in.available());
    173         } catch (IOException e) {
    174             e.printStackTrace();
    175         }
    176     }
    177 
    178     public static void main(String[] args) {
    179         String fileName = "C:/temp/newTemp.txt";
    180         ReadFile.readFileByBytes(fileName);
    181         ReadFile.readFileByChars(fileName);
    182         ReadFile.readFileByLines(fileName);
    183         ReadFile.readFileByRandomAccess(fileName);
    184     }
    185 }
    View Code
  • 相关阅读:
    ASP.NET零碎
    My97DatePicker 和转换 数据库中日期(/Date(1351699200000)/) 的格式
    生成验证码
    自己封装的AJAX (带JSON)
    WebForm
    配置进程外Session
    使用Xcode和Instruments调试解决iOS内存泄露
    IOS block 教程<转>
    iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
    支付宝在ios应用上的开发[转]
  • 原文地址:https://www.cnblogs.com/chen-kh/p/6680155.html
Copyright © 2011-2022 走看看