A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 凡沉香 中级黑马   /  2016-4-10 21:12  /  414 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

文件读取有几种方式,有总结好的吗?

2 个回复

倒序浏览
有三种,
第一种 第一种拷贝方式:通过缓冲流和字节数组相结合
第二种 通过缓冲流拷贝,不采用小数组
第三种 采用普通字节输入输出流+小数组
回复 使用道具 举报
  1. 1、按字节读取文件内容
  2. 2、按字符读取文件内容
  3. 3、按行读取文件内容

  4. 4、随机读取文件内容



  5. public class ReadFromFile {
  6.    
  7.     public static void readFileByBytes(String fileName) {
  8.         File file = new File(fileName);
  9.         InputStream in = null;
  10.         try {
  11.             System.out.println("以字节为单位读取文件内容,一次读一个字节:");
  12.             // 一次读一个字节
  13.             in = new FileInputStream(file);
  14.             int tempbyte;
  15.             while ((tempbyte = in.read()) != -1) {
  16.                 System.out.write(tempbyte);
  17.             }
  18.             in.close();
  19.         } catch (IOException e) {
  20.             e.printStackTrace();
  21.             return;
  22.         }
  23.         try {
  24.             System.out.println("以字节为单位读取文件内容,一次读多个字节:");
  25.             // 一次读多个字节
  26.             byte[] tempbytes = new byte[100];
  27.             int byteread = 0;
  28.             in = new FileInputStream(fileName);
  29.             ReadFromFile.showAvailableBytes(in);
  30.             // 读入多个字节到字节数组中,byteread为一次读入的字节数
  31.             while ((byteread = in.read(tempbytes)) != -1) {
  32.                 System.out.write(tempbytes, 0, byteread);
  33.             }
  34.         } catch (Exception e1) {
  35.             e1.printStackTrace();
  36.         } finally {
  37.             if (in != null) {
  38.                 try {
  39.                     in.close();
  40.                 } catch (IOException e1) {
  41.                 }
  42.             }
  43.         }
  44.     }

  45.    
  46.     public static void readFileByChars(String fileName) {
  47.         File file = new File(fileName);
  48.         Reader reader = null;
  49.         try {
  50.             System.out.println("以字符为单位读取文件内容,一次读一个字节:");
  51.             // 一次读一个字符
  52.             reader = new InputStreamReader(new FileInputStream(file));
  53.             int tempchar;
  54.             while ((tempchar = reader.read()) != -1) {
  55.                 // 对于windows下,/r/n这两个字符在一起时,表示一个换行。
  56.                 // 但如果这两个字符分开显示时,会换两次行。
  57.                 // 因此,屏蔽掉/r,或者屏蔽/n。否则,将会多出很多空行。
  58.                 if (((char) tempchar) != '/r') {
  59.                     System.out.print((char) tempchar);
  60.                 }
  61.             }
  62.             reader.close();
  63.         } catch (Exception e) {
  64.             e.printStackTrace();
  65.         }
  66.         try {
  67.             System.out.println("以字符为单位读取文件内容,一次读多个字节:");
  68.             // 一次读多个字符
  69.             char[] tempchars = new char[30];
  70.             int charread = 0;
  71.             reader = new InputStreamReader(new FileInputStream(fileName));
  72.             // 读入多个字符到字符数组中,charread为一次读取字符数
  73.             while ((charread = reader.read(tempchars)) != -1) {
  74.                 // 同样屏蔽掉/r不显示
  75.                 if ((charread == tempchars.length)
  76.                         && (tempchars[tempchars.length - 1] != '/r')) {
  77.                     System.out.print(tempchars);
  78.                 } else {
  79.                     for (int i = 0; i < charread; i++) {
  80.                         if (tempchars[i] == '/r') {
  81.                             continue;
  82.                         } else {
  83.                             System.out.print(tempchars[i]);
  84.                         }
  85.                     }
  86.                 }
  87.             }

  88.         } catch (Exception e1) {
  89.             e1.printStackTrace();
  90.         } finally {
  91.             if (reader != null) {
  92.                 try {
  93.                     reader.close();
  94.                 } catch (IOException e1) {
  95.                 }
  96.             }
  97.         }
  98.     }

  99.    
  100.     public static void readFileByLines(String fileName) {
  101.         File file = new File(fileName);
  102.         BufferedReader reader = null;
  103.         try {
  104.             System.out.println("以行为单位读取文件内容,一次读一整行:");
  105.             reader = new BufferedReader(new FileReader(file));
  106.             String tempString = null;
  107.             int line = 1;
  108.             // 一次读入一行,直到读入null为文件结束
  109.             while ((tempString = reader.readLine()) != null) {
  110.                 // 显示行号
  111.                 System.out.println("line " + line + ": " + tempString);
  112.                 line++;
  113.             }
  114.             reader.close();
  115.         } catch (IOException e) {
  116.             e.printStackTrace();
  117.         } finally {
  118.             if (reader != null) {
  119.                 try {
  120.                     reader.close();
  121.                 } catch (IOException e1) {
  122.                 }
  123.             }
  124.         }
  125.     }

  126.    
  127.     public static void readFileByRandomAccess(String fileName) {
  128.         RandomAccessFile randomFile = null;
  129.         try {
  130.             System.out.println("随机读取一段文件内容:");
  131.             // 打开一个随机访问文件流,按只读方式
  132.             randomFile = new RandomAccessFile(fileName, "r");
  133.             // 文件长度,字节数
  134.             long fileLength = randomFile.length();
  135.             // 读文件的起始位置
  136.             int beginIndex = (fileLength > 4) ? 4 : 0;
  137.             // 将读文件的开始位置移到beginIndex位置。
  138.             randomFile.seek(beginIndex);
  139.             byte[] bytes = new byte[10];
  140.             int byteread = 0;
  141.             // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
  142.             // 将一次读取的字节数赋给byteread
  143.             while ((byteread = randomFile.read(bytes)) != -1) {
  144.                 System.out.write(bytes, 0, byteread);
  145.             }
  146.         } catch (IOException e) {
  147.             e.printStackTrace();
  148.         } finally {
  149.             if (randomFile != null) {
  150.                 try {
  151.                     randomFile.close();
  152.                 } catch (IOException e1) {
  153.                 }
  154.             }
  155.         }
  156.     }

  157.    
  158.     private static void showAvailableBytes(InputStream in) {
  159.         try {
  160.             System.out.println("当前字节输入流中的字节数为:" + in.available());
  161.         } catch (IOException e) {
  162.             e.printStackTrace();
  163.         }
  164.     }

  165.     public static void main(String[] args) {
  166.         String fileName = "C:/temp/newTemp.txt";
  167.         ReadFromFile.readFileByBytes(fileName);
  168.         ReadFromFile.readFileByChars(fileName);
  169.         ReadFromFile.readFileByLines(fileName);
  170.         ReadFromFile.readFileByRandomAccess(fileName);
  171.     }
  172. }



  173. 5、将内容追加到文件尾部

  174. public class AppendToFile {
  175.    
  176.     public static void appendMethodA(String fileName, String content) {
  177.         try {
  178.             // 打开一个随机访问文件流,按读写方式
  179.             RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
  180.             // 文件长度,字节数
  181.             long fileLength = randomFile.length();
  182.             //将写文件指针移到文件尾。
  183.             randomFile.seek(fileLength);
  184.             randomFile.writeBytes(content);
  185.             randomFile.close();
  186.         } catch (IOException e) {
  187.             e.printStackTrace();
  188.         }
  189.     }

  190.    
  191.     public static void appendMethodB(String fileName, String content) {
  192.         try {
  193.             //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  194.             FileWriter writer = new FileWriter(fileName, true);
  195.             writer.write(content);
  196.             writer.close();
  197.         } catch (IOException e) {
  198.             e.printStackTrace();
  199.         }
  200.     }

  201.     public static void main(String[] args) {
  202.         String fileName = "C:/temp/newTemp.txt";
  203.         String content = "new append!";
  204.         //按方法A追加文件
  205.         AppendToFile.appendMethodA(fileName, content);
  206.         AppendToFile.appendMethodA(fileName, "append end. /n");
  207.         //显示文件内容
  208.         ReadFromFile.readFileByLines(fileName);
  209.         //按方法B追加文件
  210.         AppendToFile.appendMethodB(fileName, content);
  211.         AppendToFile.appendMethodB(fileName, "append end. /n");
  212.         //显示文件内容
  213.         ReadFromFile.readFileByLines(fileName);
  214.     }
  215. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马