IO--input and output stream

Input and output

• Input and output (I/O)

Read the data on the computer's hard disk into the program, which is called input, that is, input, to perform the read operation of the data

Write data from the program to the external device, which is called output, that is, output, to perform data write operation

• Subclasses of InputStream and OutputStream are byte streams

-It can read and write binary files, mainly processing audio, pictures, songs, byte streams, and the processing unit is 1 byte.

• Reader and Writer subclasses are character streams

​ Mainly deal with characters or strings, and the character stream processing unit is 1 character.

​ The byte data read by the byte stream, go to the specified encoding table to obtain the corresponding text.

Insert picture description here

public static void main(String[] args) {
    
    
        try {
    
    
            //创建FileInputStream的对象,指定要输入(读)的文件,文件不存在,抛出异常
            FileInputStream in = new FileInputStream("E:\\demo.txt");
            //每次read();一次,从输入流中读到一个字节,当读取完后会返回-1
            int b = in.read();
            System.out.println(b);
            int b1 = in.read();
            System.out.println(b1);
            int b2 = in.read();
            System.out.println(b2);
            int b3 = in.read();
            System.out.println(b3);
            int b4 = in.read();
            System.out.println(b4);
            int b5 = in.read();
            System.out.println(b5);
            int b6 = in.read();
            System.out.println(b6);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

Input and output appear in pairs

  public static void main(String[] args) {
    
    
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
    
    
            /*
            创建FileInputStream对象,指定要输入(读)的文件,文件不存在,会抛出异常
             */
            in = new FileInputStream("E:\\demo.txt");
            /*
            创建FileOutputStream对象,会自动创建输出的文件
             */
            out = new FileOutputStream("F:\\demo.txt");
            int b = 0;
            while ((b = in.read()) != -1) {
    
    
                System.out.println(b);
                out.write(b);
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //关闭流对象,释放系统资源
            try {
    
    
                if (in != null) {
    
    
                    in.close();
                }
                if (out != null) {
    
    
                    out.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }

Valid code

    public static void main(String[] args) throws IOException {
    
    
            /*
            创建FileInputStream对象,指定要输入(读)的文件,文件不存在,会抛出异常
             */
        FileInputStream in = new FileInputStream("E:\\demo.txt");
            /*
            创建FileOutputStream对象,会自动创建输出的文件
             */
        FileOutputStream out = new FileOutputStream("F:\\demo.txt");
        int b = 0;
        while ((b = in.read()) != -1) {
    
    
            System.out.println(b);
            out.write(b);
        }
        in.close();
        out.close();
    }

Improved reading and writing efficiency

package com.ff.javaio.Day2;

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

public class StreamDemo3 {
    
    
    public static void main(String[] args) throws IOException {
    
    
            /*
            创建FileInputStream对象,指定要输入(读)的文件,文件不存在,会抛出异常
             */
        FileInputStream in = new FileInputStream("E:\\demo.txt");
            /*
            创建FileOutputStream对象,会自动创建输出的文件
             */
        FileOutputStream out = new FileOutputStream("F:\\demo.txt");
        /*
        read();每次从输入流中读取一个字符  返回字符值  读完返回-1
        read(byte[] b)每次从输入流中读取一个byte数组长度个字符,返回数组中实际装入内容个数,读完返回-1
         */
        byte [] b = new byte[10];
        int length=0;
        while((length=in.read(b))!=-1){
    
    
           /* out.write(b);每次传10个字符,不足10个字符,会自动填充空格*/
            out.write(b,0,length);//向外写出一个byte数组个字节,从数组指定位置开始,写length个字节
        }
        in.read(b);
        in.close();
        out.close();
    }
}

Buffered byte in/out stream

public static void main(String[] args) throws IOException {
    
    
        //创建输入节点流,负责对文件读写
        FileInputStream in = new FileInputStream("D:\\Users\\17509\\Desktop\\Tule - Fearless.mp3");
        //创建处理对象,内部有一个缓冲数组,默认为8192个字节,包装输入流,提供缓冲功能,也可以设置缓冲区大小
        BufferedInputStream bin = new BufferedInputStream(in);

        FileOutputStream out = new FileOutputStream("E:/新建文件.mp3");
        BufferedOutputStream bout = new BufferedOutputStream(out);

       /* int b= 0;
        while ((b=bin.read())!=-1){
            bout.write(b);
        }*/
        int length = 0;
        byte[] b = new byte[1024];
        while ((length = bin.read(b)) != -1) {
    
    
            bout.write(b, 0, length);
        }
        bout.flush();//刷新缓冲区
        bout.close();
        bin.close();
    }

Guess you like

Origin blog.csdn.net/XiaoFanMi/article/details/112440159