Following on from a previous question, for some reason when I use the follo开发者_StackOverflow社区wing code :
final File tmpDir = new File("C:/TEMP/", zipFile.getName());
if(!tmpDir.mkdir() && tmpDir.exists()) {
System.err.println("Cannot create: " + tmpDir);
System.exit(0);
}
I get an error (Cannot create: C:\TEMP\aZipFile) however, if I use the following:
final File tmpDir = new File(System.getProperty("java.io.tmpdir"), zipFile.getName());
if(!tmpDir.mkdir() && tmpDir.exists()) {
System.err.println("Cannot create: " + tmpDir);
System.exit(0);
}
it works perfectly. My problem is that I want to use C:\TEMP as this is consistent with the rest of the project I am working on.
Again, I'm using Java 1.4 on Windows XP and JDeveloper IDE.
if(!tmpDir.mkdir() && tmpDir.exists())
Shouldn't this be:
if(!tmpDir.mkdir() && !tmpDir.exists())
Well, if System.getProperty("java.io.tmpdir")
does not return 'C:\TEMP' it's not the same. While I'd suggest to rely on java.io.tmpdir, you could also make sure that C:\TEMP exists - or create it if needed: ``;
File temp = new File("C:/TEMP/");
if (!temp.exists()) temp.mkdir();
File tmpDir = new File(temp, zipFile.getName());
Alternatively, you could change your code to
final File tmpDir = new File(System.getProperty("java.io.tmpdir"), zipFile.getName());
// note the change from mkdir to mkdirs
if(!tmpDir.mkdirs() && !tmpDir.exists()) {
System.err.println("Cannot create: " + tmpDir);
System.exit(0);
}
EDIT: I've just seen the answer by atomice and he's right: it's !tmpDir.exists()
rather than tmpDir.exists()
Why don't use File.createTempFile
, isn't it what you trying to archive?
Is it because you don't have write access to "C:/TEMP/"
or that TEMP folder doesn't exist?
Did you compare the result of 'System.getProperty("java.io.tmpdir")' with what you are trying? Also, on WindowsXP I'd go for "C:\Temp\" as the directory name.
Is there a file in the temp dir with the name you want that is locked?
精彩评论