I am looking for a way to make a bunch of empty txt files that would be named after elements of an ArrayList in Java.
Assuming that fList has "Apple", "Banana" and "Cherry", this piece of code should create Apple.txt, Banana.txt and Cherry.txt in the project directory.
Unfortunately, it does not, and I do not understand why. I assume it's a logic or syntax error.
public void ViewList() {
for (String fruits : fList) {
String fileName = fruits;
File f = new File(appDir + fileName + ".txt");
if (f.exists() && f.isFile()) {
System.out.println("Success!");
}
}
Can you help me understand what's wrong?
f.createNewFile()/or\depending on OS. Your codeappDir + fileName + ".txt"will result inmy/project/directoryapple.txt(notice lack of/beforeapple.txt). To solve it usenew File(appDir, fileName + ".txt");.new File(...)only creates file object which is not the same (object holds information about potential file, like path, but is not file itself). Answer in duplicate question points that out and explains that to create actual file on disc you need to invokef.createFile().