zoukankan      html  css  js  c++  java
  • Java日志第37天 2020.8.11

    字节输入流

    java.lang.InputStream

    此抽象类是表示字节输入流的所有类的超类

    所有子类中共性的方法:

    1. int read():从输入流中读取数据的下一个字节

    2. int read(byte[] b) :从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中

    3. void close():关闭此输入流并释放与该流关联的所有系统资源

    FileInputStream

    文件字节输入流

    java.io.FiIeInputStream entends InputStream

    作用:把硬盘文件中的数据读取到内存中使用

    构造方法

    - FileInputStream(String name)

    - FileInputStream(File file)

    读取一个字节

    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class Demo01InputStream {
    
        public static void main(String[] args) throws IOException {
            //创建FileInputStream对象,构造方法中绑定要读取的数据源
            FileInputStream fis = new FileInputStream("E:\Java\Practice\src\Practice\demo15\a.txt");
            //使用FileInputStream对象中的方法read,读取文件
            //read方法一次读取一个字节,读取至文件末尾返回-1
            int len = fis.read();
            System.out.println((char) len);
    
            len = fis.read();
            System.out.println((char) len);
    
            len = fis.read();
            System.out.println((char) len);
    
            //释放资源
            fis.close();
        }
    }

    结果如下:

     以上的重复代码可以使用while循环

    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class Demo01InputStream {
    
        public static void main(String[] args) throws IOException {
            //创建FileInputStream对象,构造方法中绑定要读取的数据源
            FileInputStream fis = new FileInputStream("E:\Java\Practice\src\Practice\demo15\a.txt");
            //使用FileInputStream对象中的方法read,读取文件
            //read方法一次读取一个字节,读取至文件末尾返回-1
            int len = 0;
            while((len = fis.read()) != -1){
                System.out.println((char) len);
            }
    
            //释放资源
            fis.close();
        }
    }

    读取多个字节

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Arrays;
    
    public class Demo01InputStream {
    
        public static void main(String[] args) throws IOException {
            //创建FileInputStream对象,构造方法中绑定要读取的数据源
            FileInputStream fis = new FileInputStream("E:\Java\Practice\src\Practice\demo15\a.txt");
            //使用FileInputStream对象中的方法read,读取文件
            byte[] bytes = new byte[2];
            int len = fis.read(bytes);
            System.out.println("len = "+len);
            System.out.println(new String(bytes));
    
            len = fis.read(bytes);
            System.out.println("len = "+len);
            System.out.println(new String(bytes));
    
            len = fis.read(bytes);
            System.out.println("len = "+len);
            System.out.println(new String(bytes));
    
            //释放资源
            fis.close();
        }

    结果如下:

  • 相关阅读:
    LeetCode Power of Three
    LeetCode Nim Game
    LeetCode,ugly number
    LeetCode Binary Tree Paths
    LeetCode Word Pattern
    LeetCode Bulls and Cows
    LeeCode Odd Even Linked List
    LeetCode twoSum
    549. Binary Tree Longest Consecutive Sequence II
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13485444.html
Copyright © 2011-2022 走看看