码迷,mamicode.com
首页 > 编程语言 > 详细

java IO流 之 输入流 InputString()

时间:2016-12-06 14:12:34      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:读取文件   port   java   font   microsoft   static   put   循环   缓冲   

学习java的重点之一:InputStream  字节输入流的使用

(1)FileInputstream: 子类,读取数据的通道

  使用步骤:

    1.获取目标文件:new File()

    2.建立通道:new FileInputString()

    3.读取数据:read()

    4.释放资源:close()

 

//一些默认要导入的包

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

 

 

 

 1     public static void main(String[] args) throws IOException {
 2         // TODO Auto-generated method stub
 3               
 4         //分别调用方法查看效果
 5         test1();
 6         System.out.println("-------------------------------------------");
 7         test2();
 8         System.out.println("-------------------------------------------");
 9         test3();
10         System.out.println("-------------------------------------------");
11         test4();
12     }    

 

  (2)读取数据的三种方式

    1.直接读取 (一次只能一个字节)

      int date = fileInputStream.read();

      char date3 = (char)fileInputStream.read();

 

 1 //方式一    直接打印
 2     public static void test1() throws IOException{
 3         
 4         //(1)获取目标文件路径
 5         File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
 6     
 7         //(2)根据目标文件路径 建立通道: new FileInputStream(file)
 8         FileInputStream fileInputStream = new FileInputStream(file);
 9         
10         //(3)读取数据 :read();
11         int date = fileInputStream.read();//这里是int类型
12         int date2 = fileInputStream.read();//
13         char date3 = (char)fileInputStream.read(); //以char类型显示
14         System.out.println(date+"\\"+date2+"\\"+date3);
15         
16         //(4)释放资源
17         fileInputStream.close();
18     }

 

 

 

 

    2.单独使用for循环(效率低)

      for(int i = 0; i < file.length();i++){

        System.out.print((char)fileInputStream.read());

      }

 

 1 //方式二    循环遍历
 2     public static void test2() throws IOException{
 3         
 4         //通过时间测试效率
 5         long startTime = System.currentTimeMillis();
 6         
 7         File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
 8         FileInputStream fileInputStream = new FileInputStream(file);
 9         
10         //for循环
11         for(int i = 0; i < file.length();i++){
12             System.out.print((char)fileInputStream.read());
13         }
14         
15         fileInputStream.close();
16         
17         long endTime = System.currentTimeMillis();
18         System.out.println("读取文件所花时间:"+(endTime-startTime));
19     }

 

    3.Byte[ ] 缓冲区(只能读取指定的字节数不能读取一个完整的文件)

      byte[] bt = new byte[1024];

      int count = fileInputStream.read(bt);

      System.out.println(new String (bt,0,count));

 

 1 //方式三    创建缓冲区(只能读取制定的大小,不能读取一个完整的文件)
 2     public static void test3() throws IOException{
 3         
 4         
 5         File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
 6     
 7         FileInputStream fileInputStream = new FileInputStream(file);
 8         
 9         //创建缓冲区,加快读取数据,确定要读取的字节大小
10         byte[] bt = new byte[1024];
11         
12         //read() 读取字节
13         int count = fileInputStream.read(bt);
14         System.out.println(count); //显示读取到的字节数
15         System.out.println(new String (bt,0,count));//将字节转为字符串显示
16 
17         fileInputStream.close();
18     }

 

    4.缓冲区和循环结合。缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高

      byte[] bt = new byte[1024];

      int count = 0;

      while((count = fileInputStream.read(bt)) != -1){

        System.out.println(new String (bt,0,count));

      }

 1 //方式四    循环与缓冲区结合(效率高)
 2     public static void test4() throws IOException{
 3         
 4         //通过时间测试效率
 5         long startTime = System.currentTimeMillis();
 6         
 7         File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
 8     
 9         FileInputStream fileInputStream = new FileInputStream(file);
10         
11         //缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
12         byte[] bt = new byte[1024];
13         
14         int count = 0;
15         //read返回 -1 时,证明已经遍历完
16         while((count = fileInputStream.read(bt)) != -1){
17             //字符串型显示(从bt中的第0个字节开始遍历count个长度)
18             System.out.println(new String (bt,0,count));
19         }
20         
21         fileInputStream.close();
22         
23         long endTime = System.currentTimeMillis();
24         System.out.println("读取文件所花时间:"+(endTime-startTime));
25     }    

 

陌陌说:

  在以上,对比第二个和第四个方法,会发现方法四的效率是比较高的,所以推荐使用的四个方法

  在这里我们是直接抛出异常,除了抛出之外我们还可以使用

    try{  }cater{  }finally{  }

  的方式来处理异常

java IO流 之 输入流 InputString()

标签:读取文件   port   java   font   microsoft   static   put   循环   缓冲   

原文地址:http://www.cnblogs.com/bigerf/p/6136557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!