Byte stream

Foreword : Have you ever thought about the videos we usually watch,
In what form are audio and pictures stored? In what form are they stored?


byte stream is divided into two kinds, one is output other is input Below are the top-level parents output and input streams


byte output stream : OutputStream
byte input stream : InputStream


FileOutputStrean class ( output class )

The subclass FileOutputStream of the output stream is a file output stream.

close method: close the released resources of this stream, don't waste memory.
write method (there are three usages) Let me demonstrate to you:


package com.kjzz.pojo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class day1 {
    public static void main(String[] args) throws IOException {
        // a 
        File file = new File("A.txt");
        FileOutputStream fos = new FileOutputStream(file);
        // 第一种 写出数据
        fos.write(97); // a 
        fos.write(98); // b
        // 关闭资源
        fos.close();

        // b
        FileOutputStream fo= new FileOutputStream("B.txt");
        // 第二种 字节数组
        byte [] b = "可否思否".getBytes();
        fo.write(b); // 可否思否
        // 关闭资源
        fo.close();

        // c
        // 使用文件名称创建流对象
        FileOutputStream f = new FileOutputStream("C.txt");
        byte [] c = "abQAQ".getBytes();
        // 第三种 选择字节数组di2个下标后面三个数
        f.write(c,2,3); // QAQ
        // 关闭资源
        f.close();
        
    }
}

summarizes : 1. After the stream operation, remember to use the close method release the system memory. 2. The third use of write The second is the subscript The third is to select the number of after


FileOutStream data is chase + line break operation how to use.

1. Whether to add or not is to add false (default) ture (append) after FileOutStream.

2. Line feed operation \r\n is line feed in Windows system.

import java.io.FileOutputStream;
import java.io.IOException;

public class day2 {
    public static void main(String[] args) throws IOException {

        FileOutputStream fos = new FileOutputStream("AA.txt",true);

        byte [] b = "abc".getBytes();
        
        fos.write(b);
        fos.write("\r\n".getBytes());
       
        // 关闭资源
        fos.close();
        
    }
}
输出结果:
abc(注意换行)
abc(二次运行)
abc(三次运行)    

FileInputStream class (input class)

Is a file input stream, read bytes from the file.

his method:

read: byte array read data

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

public class day3 {
    public static void main(String[] args) {
        try {
            // 文件名创建 流对象
            FileInputStream fis= new FileInputStream("BB.txt");
            // 定义变量 作为一个有效单位 0
            int len;
            // 选择单位 
            byte [] b = new byte [2];
            
            while ( (len = fis.read(b)) != -1 ) {
                System.out.println(new String(b,0,len));
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
输出结果:
ab
cd
e

summary : choose a constant of 0 and use a while loop.

InputStream output stream (read) OutputStream input stream (write).


嘻嘻硕
27 声望12 粉丝

想当一只天然呆的鸭qwq