java create file and directory if not exists code example

Example 1: java create directory if not exists

String path = ...
File pathAsFile = new File(path);

if (!Files.exists(Paths.get(path))) {
	pathAsFile.mkdir();
}

Example 2: 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();
}