java file example
Example 1: java create file
String fileData = "yourContent";
FileOutputStream fos = new FileOutputStream("yourFile.txt");
fos.write(fileData.getBytes());
fos.flush();
fos.close();
Example 2: java create file
File filename = new File(filepath)
Example 3: demo java file
import java.io.File;
class Main {
public static void main(String[] args) {
File file = new File("newFile.txt");
try {
boolean value = file.createNewFile();
if (value) {
System.out.println("The new file is created.");
}
else {
System.out.println("The file already exists.");
}
}
catch(Exception e) {
e.getStackTrace();
}
}
}