FileOutputStream file operation byte output stream
[4 types of construction methods]
<1>FileOutputStream(String filePath); [construction method parameters are directly transferred to file path] create the corresponding fileoutputstream file operation output stream object according to the path specified by the user; If the path is illegal, throw an exception FileNotFoundException();
The method used is delete write!!! The file content is cleared first, and then the data is written;
<2>FileOutputStream(File file); [construction method parameters are directly passed into file class object] create the corresponding FileOutputStream file operation output stream object according to the file class object specified by the user; If the path is illegal, throw an exception FileNotFoundException();
The method used is delete write!!! The file content is cleared first, and then the data is written;
<3>FileOutputStream(String filePath,boolean append); [construction method parameter direct input path + append write] create the corresponding fileoutputstream file operation output stream object according to the path specified by the user; If the path is illegal, throw an exception FileNotFoundException();
The append parameter is of boolean type. If the passed in parameter is true, it means [append write] and writes data at the end of the file;
<4>FileOutputStream(File file, boolean append); [construction method parameter passed in file class object + append write] create the corresponding fileoutputstream file operation output stream object according to the file class object specified by the user. If the path is illegal, an exception will be thrown: FileNotFoundException();
The append parameter is of boolean type. If the passed in parameter is true, it means that [append write] writes data at the end of the file;
[3 types of member methods]
<1>void write(int b); Write one byte of data into the file;
<2>void write(byte[] buf); Write a byte array to the file;
<3>void write(byte[] buf,int off,int count); Write a byte array to the file. Count from the off offset position; [confirm the range of data written to the array]
[operation procedure]
<1> Specify the path of the corresponding File. You can choose to directly give the corresponding String type path, or create the corresponding File class object as a parameter;
<2> Create a FileOutputStream file operation output byte stream and open the file operation pipeline;
<3> The FileOutputStream object writes data to a file;
<4> Close resources;
[note]
<1> Fileoutputstream has the ability to create files. Files can be created when the path is legal and the corresponding directory has write permission
<2> Distinguish between delete write and append write;
class Demo10 { public static void main(String[] args) throws IOException { //Create FileOutputStream class object [construction method direct incoming path] //No append is passed in, so delete write is the default //[delete write] means that when creating FileOutputStream class objects, all the original contents in the file will be deleted when writing //However, if the same FileOutputStream class object calls the write() method multiple times, it is an additional write; FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\123\\Desktop\\file\\file.txt"); //Write a byte of data to the file fileOutputStream.write(49); //Write a byte array data and write it to the file, [unspecified] start subscript and write quantity String write = "Xiaoha, Xiaojin, Xiaohua"; //[convert String format to byte [] array] use getButes() method byte[] bytes = write.getBytes(); fileOutputStream.write(bytes); //Write a byte array data into the file, and [specify] start subscript and write quantity fileOutputStream.write(bytes,0,25); //close resource fileOutputStream.close(); //Create a file object and pass in the append parameter [append write] File file = new File("C:\\Users\\123\\Desktop\\file\\file.txt"); //Create FileOutputStream class object [File class object passed in by constructor] FileOutputStream fileOutputStream1 = new FileOutputStream(file,true); //Append write fileOutputStream1.write(bytes); //close resource fileOutputStream1.close(); } }