create file in folder java code example

Example 1: java create file in folder

File file = new File("src/resources/run.txt");

file.getParentFile().mkdirs();

FileWriter writer = new FileWriter(file);
writer.append("this is a test");
writer.close();

Example 2: java create file in folder

File file = new File("src/resources/run.txt");

file.getParentFile().mkdirs();

file.createNewFile();

Example 3: creating file in specific location in java application

String path = "C:" + File.separator + "hello" + File.separator + "hi.txt";
// Use relative path for Unix systems
File f = new File(path);

f.getParentFile().mkdirs(); 
f.createNewFile();

Tags:

Java Example