I'am facing a weird issue in our dev domains . So basically our java app currently runs on jdk1.6 , we are planning to upgrade that to 1.8 . So currently the following code works fine in 1.6 but returns an exception
java.io.IOException: Unable to create temporary file, C:\Users\xxxxx\xxxxx\Local\Temp\XYZDirectory
at java.io.File$TempDirectory.generateFile(File.java:1921)
at java.io.File.createTempFile(File.java:2010)
at java.io.File.createTempFile(File.java:2070)
in 1.8 . Code is as follows -
File file = null;
try {
file =
File.createTempFile("XYZ", "Directory" + System.getProperty("file.separator"));
file.delete();
file.mkdirs();
} catch (final IOException e) {
e.printStackTrace();
}
return file;
We want our product to be compatible with both 1.6 and 1.8 .
After some research i found that i might have to use the following method of Files
class
public static Path createTempDirectory(Path dir,
String prefix,
FileAttribute<?>... attrs)
throws IOException
So i have the following queries -
1) Why File.createTempFile
throws an Exception in 1.8 ?
2) What is the conceptual difference between the two methods ?
3) More over if File.createTempFile
is no longer supported why it is not deprecated ?
4) What is the suitable way to address this issue ? In other words i can do a programmatic check to use appropriate method on the basis of jdk version installed in the VM and then proceed with the creation of temporary directory , but is this best way to to address this issue ?