The use of FileOutputStream class in Java

Table of contents

1. The role of the FileOutputStream class

2. Steps to use the FileOutputStream class

(1) Import packages of related classes

(2) Create a FileOutputStream class object

(3) Write data

(4) Shutdown


1. The role of the FileOutputStream class

write data to file

2. Steps to use the FileOutputStream class

(1) Import packages of related classes

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

(2) Create a FileOutputStream class object

At this time, the exception will be displayed. You can click the red wavy line, use the shortcut key alt+enter, and select the first one to add the exception to the method signature , or add the exception to the method signature yourself.

You can create a FileOutputStream class object

public class FileOutputStreamDemo {
    public static void main(String[] args) throws FileNotFoundException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        
    }
}
Note: 
When creating a byte output stream object, the parameter can be a path represented by a string or a File object. 
If the file does not exist, a new file will be created, but its parent path must exist. 
If the file already exists, it will be created. Empty the file (that is, newly written data will overwrite the previous content)

(3) Write data

Call the write method to write data

(a). void write(int b) writes one byte of data at a time

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //IOException是FileNotFoundException的父类,因此只写IOException即可
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        //传入的是int类型数据,但实际写到文件中的是整数在ASCII上对应的字符
        fos.write(97);
        //相同的,抛出异常
    }
}

(b). void write(byte[] b) writes a byte array data at a time

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        byte[] bytes1 = {97,98,99,100,101};
        fos.write(bytes1);
        //一次写入一个字节数组数据
    }
}

(c). void write(byte[] b, int off, int len) writes part of the data of a byte array at a time

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        byte[] bytes1 = {97,98,99,100,101};
        fos.write(bytes1,2,4);
        //off:起始字节索引,len:写入的字节个数

    }
}

 Newline write data

To implement newline writing data, just write a newline character

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        //创建一个字符串,内容为换行符
        fos.write(97);
        String str1 = "\r\n";//也可以只写\r或者\n但最好写完整
        //调用getBytes()方法,将字符串中内容转换为字节数组
        byte[] bytes1 = str1.getBytes();
        fos.write(bytes1);
        //同样的,可以用字符串的方式创建较长的数据,再调用getBytes()方法,转换为字节数组
        String str2 = "asdfghjklqwert";
        byte[] bytes2 = str2.getBytes();
        fos.write(bytes2);
    }
}

continue writing

If you want to implement continuous writing (write new data after the original data of the file), you only need to pass in the second parameter true when creating the file output stream object

 can be continued

(4) Shutdown

Close the stream every time after using the stream, that is, disconnect the link between the program and the file

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        byte[] bytes1 = {97,98,99,100,101};
        fos.write(bytes1,2,4);
        //关流
        fos.close();
    }
}

The following is the complete code for the continuation, and the rest of the code basically only needs to add the closing flow.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //创建输出流对象
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt",true);
        //写入续写数据
        String str = "asdfghjklqwert";
        byte[] bytes = str.getBytes();
        fos.write(bytes);
        //关流
        fos.close();
    }
}

 

Guess you like

Origin blog.csdn.net/2301_76161469/article/details/129978197