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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© micro_hx 中级黑马   /  2015-6-13 16:53  /  496 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  由于刚刚初学java,很多都不懂,新手勿喷。。
  下面是自己总结的几个文件读取的例子,都没有使用try catch,异常全都抛出了。。。
  1. package com.test1;

  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileReader;
  6. import java.io.InputStream;
  7. import java.io.Reader;

  8. import org.junit.Test;

  9. /**
  10. * 各种读取文件的方式
  11. * @author Administrator
  12. */
  13. public class Test2 {

  14.         private final static String FILE_PATH = "D:/Test.java" ;
  15.        
  16.         /**
  17.          * 使用的是read()方法, 使用的是其子类FileReader
  18.          * @throws Exception
  19.          */
  20.         @Test
  21.         public void testReader() throws Exception {
  22.                 Reader reader = new FileReader(new File(FILE_PATH));
  23.                 int len = 0 ;
  24.                 while((len = reader.read()) != -1){
  25.                         System.out.print((char)len);
  26.                 }
  27.                 reader.close();
  28.         }
  29.        
  30.         /**
  31.          * 使用是read(buf)方法,使用的是其子类FileReader
  32.          * @throws Exception
  33.          */
  34.         @Test
  35.         public void testReader2()throws Exception{
  36.                 Reader reader  = new FileReader(new File(FILE_PATH));
  37.                 char[] but = new char[1024];
  38.                 int len = 0 ;
  39.                 while((len=reader.read(but)) != -1){
  40.                         System.out.println(new String(but,0,len));
  41.                 }
  42.                 reader.close();
  43.         }
  44.        
  45.         /**
  46.          * 使用readLine()方法,该方法在读取效率中最高,因为是整行整行读取,
  47.          * 使用的是BufferedReader类
  48.          * @throws Exception
  49.          */
  50.         @Test
  51.         public void testReader3()throws Exception{
  52.                 BufferedReader reader = new BufferedReader(new FileReader(new File(FILE_PATH)));
  53.                 String len = null ;
  54.                 while((len = reader.readLine()) != null){
  55.                         System.out.println(len);
  56.                 }
  57.                 reader.close();
  58.         }
  59.        
  60.         /**
  61.          *是read()方法,但是使用的是InputStream的子类FileInputStream
  62.          * @throws Exception
  63.          */
  64.         @Test
  65.         public void testReader4() throws Exception{
  66.                 InputStream input = new FileInputStream(new File(FILE_PATH));
  67.                 int temp = 0 ;
  68.                 while((temp = input.read()) != -1){
  69.                         System.out.print((char)temp);
  70.                 }
  71.                 input.close();
  72.         }
  73.        
  74.         /**
  75.          * 使用的是read(byte[]) 方法,使用的是InputStream的子类中的FileInputStream类
  76.          * @throws Exception
  77.          */
  78.         @Test
  79.         public void testReader5() throws Exception{
  80.                 InputStream input = new FileInputStream(new File(FILE_PATH));
  81.                 byte[] but = new byte[1024];
  82.                 int i = 0 ;
  83.                 while((i = input.read(but)) != -1){
  84.                         System.out.println(new String(but,0,i));
  85.                 }
  86.                 input.close();
  87.         }
  88.        
  89.        
  90.        
  91. }
复制代码
当然,方法远远不止这些,希望大神看到后,能补上自己的方法,大家相互交流,可能今后还要在同一个班学习了,这里先表态了啊。。。。


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马