workplace-blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.流的概念

  • 内存与存储设备间传输数据的通道。
  • 数据通过流传输------->水通过水管传输。

2.流的分类

  • 按方向分【重点】
    • 输入流:将存储设备中的内容读入到内存中。
    • 输出流:将内存中的内容读入到存储设备。

  • 按单位分
    • 字节流:传输的数据以字节为单位,可以读取所有数据。
    • 字符流:传输的数据以字符为单位,只能读取文本数据。
  • 按功能分
    • 节点流:具有实际传输数据的读写功能。
    • 过滤流:在节点流的基础上增强功能。

3.字节流

  • 字节流的父类(抽象类):

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

    需要定义 InputStream 子类的应用程序必须总是提供返回下一个输入字节的方法。

  • 方法摘要
    int available() 返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。
    void close() 关闭此输入流并释放与该流关联的所有系统资源。
    void mark(int readlimit) 在此输入流中标记当前的位置。
    boolean markSupported() 测试此输入流是否支持 markreset 方法。
    abstract int read() 从输入流中读取数据的下一个字节。
    int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
    int read(byte[] b, int off, int len) 将输入流中最多 len 个数据字节读入 byte 数组。
    void reset() 将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。
    long skip(long n) 跳过和丢弃此输入流中数据的 n 个字节。
  • OutputStream:此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。

    需要定义 OutputStream 子类的应用程序必须始终提供至少一种可写入一个输出字节的方法。

  • 方法摘要
    void close() 关闭此输出流并释放与此流有关的所有系统资源。
    void flush() 刷新此输出流并强制写出所有缓冲的输出字节。
    void write(byte[] b)b.length 个字节从指定的 byte 数组写入此输出流。
    void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
    abstract void write(int b) 将指定的字节写入此输出流。

文件字节流

  • FileInputStream:

    • 从流中读取多个字节,将读到的内容放入b数组,返回实际读到的字节数,如果达到文件的尾部,则返回-1
    public int read(byte[] b)
    
    //文件字节输入流使用
    public class fileInputStream {
        public static void main(String[] args) throws IOException {
            //1.创建实例
            FileInputStream fins = new FileInputStream("E:\\test.txt");
    
            //2.读取
            //2.1单个字节读取
            int data = 0;
            while ((data=fins.read())!=-1){
                System.out.print((char)data);
            }
    
            //2.2多个字节读取
            byte[] buf = new byte[3];
            int count = 0;
            while ((count =fins.read(buf)) != -1) {
                System.out.println(new String(buf,0,count));
            }
        }
    }
    //输出结果:
    abcdefg
    abc
    def
    g
    
  • FileOutputStream

    • 一次写多个字节,将b数组中的所有字节写入输出流
    public int write(byte[] b)
    
    /**
     * 文件字节输出流:FileOutputStream
     */
    public class fileOutputStream  {
        public static void main(String[] args) throws Exception{
            //append:文件内容覆盖,默认为false进行覆盖。
            FileOutputStream fos = new FileOutputStream("E:\\out.txt",false);
            fos.write(97);
            fos.write('a');
    
            String s = "hello world";
            fos.write(s.getBytes());
    
            System.out.println("finish");
        }
    }
    
  • 字节流复制文件

/**
 * 复制一个文件
 */
public class copyFile {
    public static void main(String[] args) throws Exception{
        //1.创建流
        //1.1.创建文件输入流
        FileInputStream fis = new FileInputStream("E:\\test.txt");
        //1.2.创建文件输出流
        FileOutputStream fos = new FileOutputStream("E:\\copy.txt");
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count = fis.read(buf)) !=-1 ){
            fos.write(buf,0,count);
        }
        fis.close();
        fos.close();
        System.out.println("finish");
    }
}
posted on 2022-08-26 19:37  多巴胺LLL  阅读(49)  评论(0)    收藏  举报