Answers for "java how to create a file if not exist"

1

java create file if not exists

String yourFile = "YourFile.txt";
String yourContent = "YourContent"

File tmpDir = new File(yourFile);
boolean exists = tmpDir.exists();

if(!exists) {
	FileOutputStream fos = new FileOutputStream(yourFile);
	fos.write(yourContent.getBytes());
	fos.flush();
	fos.close();
}
Posted by: Guest on April-26-2020
0

java create file if not exists

File yourFile = new File("score.txt");
yourFile.createNewFile(); // if file already exists will do nothing 
FileOutputStream oFile = new FileOutputStream(yourFile, false);
Posted by: Guest on August-27-2021

Code answers related to "java how to create a file if not exist"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language