- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4
Java Create File with FileOutputStream
        Ramesh Fadatare edited this page Jul 11, 2019 
        ·
        1 revision
      
    In this example, we create a new, empty file with FileOutputStream.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class JavaCreateFileExample {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileOutputStream fout = null;
        try {            
            fout = new FileOutputStream("src/main/resources/myfile.txt");
        } finally {            
            if (fout != null) {
                fout.close();
            }
        }
    }
}The file is created when FileOutputStream object is instantiated. If the file already exists, it is overridden.
FileNotFoundException is thrown if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.