FileReader读取文件内容!

 public static void main(String []args)throws Exception
   {
      try
      {
           File file = new File("test.txt") ;
           int fileSize = (int)file.length();
           int charsReaded = 0;
           FileReader in = new FileReader(file);//建立当前要打开的文件句柄
           char[] data = new char[fileSize]; //建立一个字符数组,用于存放
           while(in.ready())
           {
               charsReaded += in.read(data, charsReaded, fileSize - charsReaded);  
           }
           System.out.println("显示文件内容:从开头显示到结尾");
           String s1=new String(data, 0, charsReaded);//把纯字符数组转变为String类型
           System.out.println(s1); //String类型可直接输出 
           System.out.println("显示文件内容:10个字节");
           in.close();    //关闭文件    

//一行一行读取

 File f = new File("E://test.txt");
         InputStreamReader read = new InputStreamReader(new FileInputStream(f), "GBK");
         BufferedReader reader = new BufferedReader(read);
         String line;
         String[] a = new String[100000];
//         line = reader.readLine();
         int i;
         //按行取出所有文本
         for (i = 0; i < 100000; i++) {
            if ( (line = reader.readLine()) != null) {
//               a[i] = line;
//               System.out.println(a[i]);
//               System.out.println(i);
               System.out.println("测试:"+line);
            }

         }

//

BufferedReader br1 = new BufferedReader(new FileReader("E://test.txt"));
         String str = "";
            System.out.println("E://test.txt行开始!");
         while ( (str=br1.readLine()) != null) {
            System.out.println("读取:"+str);
         }
         br1.close();
         br1 = null;
         System.out.println("读取文件完毕!");
      }
      catch (IOException e)
             
          System.out.println("文件打开错误!"+fileName);    
      }
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值