zoukankan      html  css  js  c++  java
  • java IO流 之 字节输入流 InputString()

    学习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\bg\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\bg\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\bg\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\bg\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{  }

      的方式来处理异常

  • 相关阅读:
    MarkDown SequenceDiagram 语法
    mysql导出数据库文档
    使用gitlab作为go mod私服
    go context理解
    go-micro入门
    golang 接口测试
    小程序配置 app.json
    Nginx 配置详解(2)
    windows下安装nginx
    任意文件夹下打开命令提示窗
  • 原文地址:https://www.cnblogs.com/bigerf/p/6136557.html
Copyright © 2011-2022 走看看