Input stream and output stream in IO stream

Input stream [InputStream]

Explanation: Input stream refers to the operation of reading data ( referenced to memory ).

Construction method:

FileInputStream(File file):

Create one by opening a connection to the actual file specified by an object FileInputStreamin the filesystem .Filefile

FileInputStream(FileDescriptor fdObj):

fdObjCreate one by using a file descriptor FileInputStreamrepresenting an existing connection to an actual file in the file system. (This method is generally not used)

FileInputStream(String name):

Create one by opening a connection to the actual file specified FileInputStreamby a pathname in the file system name.

Method summary: 


 

 Common methods:

read():

Format: variable name.read()

Returns -1 when nothing is read.

File to read: 

Since this method can only read one byte at a time, a loop is required to fully read the data 

    public static void main(String[] args) throws Exception {
        File f1 = new File("D:/aa/bb/cc/c.txt");
        //创建输入流
        InputStream in = new FileInputStream(f1);

        //使用循环遍历数据
        int n;
        while ((n = in.read()) != -1){
            System.out.println(n);
        }
    }

result:

 

Note: Because this method returns int type data, you need to check the ascll table to restore the data. 

It can also be converted directly:

    public static void main(String[] args) throws Exception {
        File f1 = new File("D:/aa/bb/cc/c.txt");
        InputStream in = new FileInputStream(f1);

        int n;
        while ((n = in.read()) != -1){
            System.out.println((char)n);
        }
    }

 

 

read(byte[] b)

Format: variable name.read (byte array name)

 Returns -1 when nothing is read.

 

public static void main(String[] args) throws Exception {
        File f1 = new File("D:/aa/bb/cc/c.txt");
        //创建输入流
        InputStream in = new FileInputStream(f1);
        //创建数组接受数据
        byte[] bytes = new byte[10];

        //使用循环遍历数据
        int n;
        while ((n = in.read(bytes)) != -1){
            //将数组按照ascll表转换为字符串
            String s = new String(bytes,0,n);
            System.out.print(s);//由于不能改变数据格式所以此处不要ln
        }
    }

 

 

Output stream [OutputStream]

Explanation: Input stream refers to the operation of writing data (referenced to memory).

Construction method:

FileOutputStream(File file)

 Creates a Filefile output stream that writes data to the file represented by the specified object.

FileOutputStream(File file, boolean append)

Creates a Filefile output stream that writes data to the file represented by the specified object.

FileOutputStream(FileDescriptor fdObj)
Creates an output file stream that writes to the specified file descriptor representing an existing connection to an actual file in the file system.

FileOutputStream(String name)
Creates an output file stream that writes data to the file with the specified name.

FileOutputStream(String name, boolean append)
Creates an nameoutput file stream that writes data to the file with the specified .

Method summary: 

 

 Common methods:

wite(byte[] b):

public static void main(String[] args) throws Exception {
        File f1 = new File("D:/aa/bb/cc/c.txt");

        //创建输出流
        OutputStream out = new FileOutputStream(f1);
        //设置输出内容并转换成byte数组
        String str = "qwerty";
        byte[] bytes = str.getBytes();
        //在指定文件写入内容
        out.write(bytes);
        //关闭资源
        out.close();
    }

result:

 Comprehensive case: copy the contents of c.txt to d.txt.

 

 public static void main(String[] args) throws Exception {
        File f1 = new File("D:/aa/bb/cc/c.txt");
        File f2 = new File("D:/aa/bb/cc/d.txt");

        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);
        byte[] bytes = new byte[10];
        int n;
        while ((n = in.read(bytes)) != -1) {
            out.write(bytes,0,n);
        }
    }

result:

 

Guess you like

Origin blog.csdn.net/weixin_69420643/article/details/128276196