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 a 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.
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course