I currently have the problem that I encounter an exception I never saw before and that's why I don't know how to handle it.
I want to create a file according to given parameters, but it won't work.
public static Path createFile(String destDir, String fileName) throws IOException {
FileAccess.createDirectory( destDir);
Path xpath = new Path( destDir + Path.SEPARATOR + fileName);
if (! xpath.toFile().exists()) {
xpath.toFile().createNewFile();
if(FileAccess.TRACE_FILE)Trace.println1("<<< createFile " + xpath.toString() );
}
return xpath;
}
public static void createDirectory(String destDir) {
Path dirpath = new Path(destDir);
if (! dirpath.toFile().exists()) {
dirpath.toFile().mkdir();
if(TRACE_FILE)Trace.println1("<<< mkdir " + dirpath.toString() );
}
}
Every time I run my application the following exception occurs:
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
[...]
How do I get rid of it? (I am using Win7 64bit btw)
The problem is that a file can't be created unless the entire containing path already exists - its immediate parent directory and all parents above it.
If you have a path c:\Temp and no subdirectories below it, and you try to create a file called c:\Temp\SubDir\myfile.txt, that will fail because C:\Temp\SubDir doesn't exist.
Before
xpath.toFile().createNewFile();
add
xpath.toFile().mkdirs();
(I'm not sure if mkdirs() requires just the path in the object; if it does, then change that new line to
new File(destDir).mkdirs();
Otherwise, you'll get your filename created as a subdirectory instead! You can verify which is correct by checking your Windows Explorer to see what directories it created.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With