Introduce IO input stream, output stream and introduce the basic usage of both

1. What is IO
Concept:It is the input and output of data based on streams. In layman's terms, Java communicates with the outside world through data streams.
The operation of data in IO is through streams. We can think of data flow as water flow. It can flow from one source to another destination, and the water flow does not arrive all at once. And continuously flows into the destination. I refers to the flow from the data stream of the external device into the Java program, and o refers to the stream that outputs the data stream from the Java program to other devices.
Two steps of IO operation
1. Prepare data
2. Copy data from the kernel to the program process a>

Classification of IO

According to the flow direction of data, it is divided into:input stream andoutput stream

Input stream input:Read external data (data from storage devices such as disks and optical disks) in the program (memory).

Output stream output: A stream that outputs program (memory) data to storage devices such as disks and optical disks.

According to operationData unit is divided into: byte stream (8 bit), character stream (16 bit).

According to the direction of data flow, it is divided into: input stream and output stream.

According to the different roles of the flow, it is divided into: node flow and processing flow.

2. Byte stream

Concept: InputStream and OutputStream are two abstract classes, which are the base classes of byte streams. All specific byte stream implementation classes are Inherited from these two classes respectively.

Byte input stream:

Byte input stream [InputStream] java, io.Inputstream abstract class is the super class of all classes representing byte input streams, which can read byte information into memory. It defines the basic common functional methods of byte input streams.

public void close(): Closes this input stream and releases any system resources associated with this stream.

public abstract int read() : Read the next byte of data from the input stream.

public int read(byte[] b) : Read some number of bytes from the input stream and store them into byte array b.

close method. When the stream operation is completed, this method must be called to release system resources.

/**
 * @author 高影  2022/12/13  11:57
 * @version 1.0
 */
public class Demo03 {
    public static void main(String[] args) throws IOException {
        //输出流:把内容的内存输出到文件中(写操作) 输入流:把文件的内容输入到内存中(读操作)
        File f1 = new File("D:/a.txt");
        f1.createNewFile();
        String s = "abcde";
        OutputStream out = new FileOutputStream(f1);
        out.write(s.getBytes());
        //定义输入流来读取文件内容
        InputStream in = new FileInputStream(f1);

        //读一个字节,把当前字符转换成对应的整数 返回
        int c1 = in.read();
        System.out.println(c1);

    }
}

Byte output stream:

Byte output stream [OutputStream] java.io.0utputStream abstract class is the super class of all classes that represent byte output streams, writing out the specified byte information to the destination. It defines the basic common functional methods of byte output streams.

public void close() : Closes this output stream and releases any system resources associated with this stream.

public void flush() : Flushes this output stream and forces any buffered output bytes to be written out.

public void write(byte[] b) : Write b.length bytes from the specified byte array to this output stream.

public void write(byte[] b, int off, int len): Write len bytes from the specified byte array and output to this output stream starting from offset off.

public abstract void write(int b): Output the specified bytes to the stream.

/**
 * @author 高影  2022/12/13  10:49
 * @version 1.0
 */
public class Demo01 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:/aa");
        File f2 = new File(f1,"a.txt");
        f1.mkdirs();
        f2.createNewFile();

        //使用OutPutStream输出流完成对文件的写入操作  内存输出到文件
        OutputStream out = new FileOutputStream(f2); //定义一个输出流,输出到f2文件中
        //out.write(97);//按照ASCII解析数字 写入文件中  写入的是97对应的 a
        byte[] bytes = {97,98,99,100,101,102,103};
        //out.write(bytes); //按照ASCII解析数字 写入文件中  写入的是:abcdefg
        out.write(bytes,2,3); // 从数组 指定索引位置2 写入3个元素

    }
}

 

/**
 * @author 高影  2022/12/13  11:05
 * @version 1.0
 */
public class Demo02 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:/来自一个爱你的表白");
        f1.createNewFile();

        OutputStream out = new FileOutputStream(f1);

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入:");
        String msg = sc.next();
        byte[] bytes = msg.getBytes(); //获取字符串对应解析后的byte数组
        out.write(bytes);

    }
}

 Comprehensive case: complete file copying

Create a new file a.txt in the D drive and write the content in it

/**
 * @author 高影  2022/12/13  13:17
 * @version 1.0
 */
public class Demo04 {
    public static void main(String[] args) throws IOException {
        //创建文件对象
        File f1 = new File("D:/a.txt");
        f1.createNewFile(); //创建文件
        String s = "还和以前一样好好的"; // 准备写入的数据
        OutputStream out = new FileOutputStream(f1); //创建输出对象
        out.write(s.getBytes());

        //创建输入流对象
        InputStream in = new FileInputStream(f1);
        //获取字节返回一个int类型的变量
        int read = in.read();
        //初始化n
        int n;
        //使n=read值,当n不等于-1时,则打印出n的值
        while ((n=in.read()) !=-1){
            System.out.println(n);
        }
    }
}
/**
 * @author 高影  2022/12/13  13:17
 * @version 1.0
 */
public class Demo05 {
    public static void main(String[] args) throws IOException {
        //定义源文件和目标文件的File对象
        File f1 = new File("D:/a.txt");
        File f2 = new File("D:/f2.txt");

        //定义输入输出流关联文件
        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);

        byte[] b = new byte[10];
        int n;
        while ((n=in.read(b)) !=-1){
            out.write(b,0,n);
        }
    }
}

 

 

 

Guess you like

Origin blog.csdn.net/weixin_69036336/article/details/128276247