Construction method of FileOutputStream
FileOutputStream Four common construction methods are provided for instantiation FileOutputStream Object, Different scenarios use different construction methods.
Scenario 1: use the File object to open a local File and read data from the File.
public FileOutputStream(File file) throws FileNotFoundException{}
Check the underlying source code and find that the constructor actually calls another constructor
public FileOutputStream(File file) throws FileNotFoundException { this(file, false); }
Scenario 2: directly pass in the File path without using the File object.
public FileOutputStream(String name) throws FileNotFoundException{}
The construction method of FileOutputStream allows you to pass in a File path directly without using a File object. View the source code of the construction method. The File object is used internally to open the File.
Scenario 3: open the file and write data at the end of the file.
Scenarios require writing data at the end of the file. Since the first two constructors start writing data from the file (overwriting the original file), the constructors of the first two scenarios cannot be used. FileOutputStream provides two other constructors:
public FileOutputStream(File file,boolean append) throws FileNotFoundException{} public FileOutputStream(String name,boolean append) throws FileNotFoundException{}
Compared with the previous construction methods, the two construction methods each have a boolean parameter append.
When the append parameter is true, data is written from the end of the file; When the append parameter is false, the data overwrites the original file.
This is also the method called by the first method
Write method of FileOutputStream
FileOutputStream Class provides a variety of file writing methods. You can write a byte to a file separately, You can also write one byte Array to file, or get byte Part of the data of the array is written to the file.
Example 1: use the write(int b) method to write a file.
package com.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo { public static void main(String[] args) throws IOException { File file = new File("d://new.txt"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { FileOutputStream fileOutputStream = new FileOutputStream(file); String str = "this is new file"; for (int i = 0; i < str.length(); i++) { int b = (int)str.charAt(i); fileOutputStream.write(b); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
The example program first calls createNewFile() of the File class to create a new.txt File, and then writes the str content to the newly created new.txt File.
Example 2: use the write(byte[] b) method to write a file.
The write(byte[] b) method is used to write b.length bytes from the specified byte array to the output stream.
The getBytes() method of the String class can convert a String into a byte array. Use the write(byte[] b) method of the FileOutputStream class to write the converted byte array to a file.
package com.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo { public static void main(String[] args) throws IOException { File file = new File("d://new.txt"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { FileOutputStream fileOutputStream = new FileOutputStream(file); String str = "this is new file"; fileOutputStream.write(str.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Example 3: use the write(byte[] b,int off,int len) method to write a file.
This method writes len bytes of data to the output stream from the off position of array b.
package com.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo { public static void main(String[] args) throws IOException { File file = new File("d://new.txt"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { FileOutputStream fileOutputStream = new FileOutputStream(file); String str = "this is new file"; fileOutputStream.write(str.getBytes(),5,11); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
The program writes the specified STR content to the file. The first parameter of fos.write(str.getBytes(),5,10) statement is the byte array, the second parameter 5 starts from the subscript 5 of the byte array, and the third parameter is the number of bytes written. After the program is executed, the content written is "is new file".
When using this method, you must pay attention to the problem of array out of bounds. For example, if the length of byte array is 20 and 15 bytes are written to the file from subscript 12, the array will be out of bounds and the program will report an error.
Example 4: copying files using FileOutputStream
package com.demo; import java.io.*; public class Demo { public static void main(String[] args) { File source = new File("d://new.txt"); File dest = new File("d://new2.txt"); copy(source,dest); } public static void copy(File sor, File dest){ if(!sor.exists()){ System.out.println("The source file does not exist"); return; } try { FileInputStream in = new FileInputStream(sor); FileOutputStream out = new FileOutputStream(dest); byte[] buf = new byte[in.available()]; in.read(buf); out.write(buf); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Copying a file is to write the source file data to a new file. In actual programming, there are many methods to copy files. In this case, FileInputStream and FileOutputStream are used to copy files.