zoukankan      html  css  js  c++  java
  • FileInputStream

    InputStream 基类,抽象类

        FileInputStream  读取文件的字节流

        BufferedInputStream  缓冲输入字符流

     1 package file;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.IOException;
     6 
     7 public class test {
     8     public static void main(String[] args) throws IOException {
     9 //        readTest1();    
    10 //        readTest2();    
    11 //        readTest3();
    12         readTest4();    //效率最高
    13     }
    14     
    15     //方式4:使用缓冲数据配合循环读取
    16     public static void readTest4() throws IOException  {
    17         long startTime = System.currentTimeMillis();
    18         File file = new File("F:\a.txt");
    19         FileInputStream fileInputStream = new FileInputStream(file);
    20         //建立缓冲字节数组,读取文件的数据。
    21         byte[] buf = new byte[1024];
    22         int content = 0;
    23         while((content = fileInputStream.read(buf) ) != -1){
    24             System.out.println("内容是:"+ content);
    25         }
    26         fileInputStream.close();
    27         long endTime = System.currentTimeMillis();
    28         System.out.println("读取时间是:"+ (endTime-startTime));
    29     }
    30     
    31     //方式3、使用 缓冲数据 读取    无法读取完整一个文件的数据:
    32     public static void readTest3() throws IOException  {
    33         File file = new File("F:\a.txt");
    34         FileInputStream fileInputStream = new FileInputStream(file);
    35         //建立缓冲字节数组,读取文件的数据。
    36         byte[] buf = new byte[1024];
    37         int length = fileInputStream.read(buf);    //数据已经存储到了字节数组中了,read返回值是本次读取了几个字节数据到字节数组中
    38         System.out.println("length:" + length);
    39         //使用字节数组构建字符串
    40         String content = new String(buf,0,length);    //读取长度个,一定要加length,因为不加length,会覆盖,比如,aaaabbb,读取之后会变成aaaabbba。
    41         System.out.println("内容是:"+content);
    42         fileInputStream.close();
    43     }
    44     
    45     //方式2、使用循环读取文件的数据
    46     public static void readTest2() throws IOException  {
    47         File file = new File("F:\a.txt");
    48         FileInputStream fileInputStream = new FileInputStream(file);
    49         //读取文件的数据
    50         int content = 0;
    51         while((content = fileInputStream.read())!=-1) {
    52             System.out.print((char)content);
    53         }
    54         fileInputStream.close();
    55     }
    56     
    57     //方式1、
    58     public static void readTest1() throws IOException {
    59         //1.找到目标文件
    60         File file = new File("F:\a.txt");
    61         //建立数据的输入通道
    62         FileInputStream fileInputStream = new FileInputStream(file);
    63         //读取文件中的数据
    64         int content = fileInputStream.read();    //每次读取一个字节
    65         System.out.println("读取的内容.."+ content);
    66         //关闭资源
    67         fileInputStream.close();
    68     }
    69 }
  • 相关阅读:
    linux [Fedora] 下的 "飞秋/飞鸽传书"
    弹跳是不是自由落体?
    插件的简单原理
    WebService的简单应用
    普通按钮的另一种提交方式(调用后台事件)
    ASPNET服务端控件练习(一个机试题)
    AJAX简单的数据增删改与分页应用
    new XMLHttpRequest()和页面关系
    c++中placement new
    netty的引用计数
  • 原文地址:https://www.cnblogs.com/linst/p/5659747.html
Copyright © 2011-2022 走看看