I was here tackling this same problem and I finally solved it.
The goal is to create a file (example a .txt file) within a folder.
Things you'll need:
- Folder name
Instead of this:
File f = new File("C:/a/b/test.txt");
Try
//get the file's absolute path. You could input it yourself
// but I think it is better to have a function to handle
// system rules on how to format the path string
String myFolder = "b";
String myFile = "test.txt";
String folderPath = myFolder.getAbsolutePath(); //will get the entire path for you
String pathToFile = folderPath + File.separator + myFile;
// this will probably throw a exception, you want to make sure you handle it
try {
File newFile = new File(pathToFile);
if (newFile.createNewFile()) {
System.out.println("bwahahah success");
}
else {
System.out.println("file creation failed");
}
}catch(IOException e) {
System.out.println(e.getMessage()); // you will need to print the message in order to get some insight on what the problem is.
}
// you can also add a throw if within the if statement with other checks in order to know where the error is coming from, but to keep it short this is the gist of it.
// Really hope this will assist anyone that stumbles upon this same issue.
Other resources for further reading: everything on java paths & files
Please comment if there is anything I might also overlook, lemme absorb some of that knowledge