TEMP file question
Hi all,
In my application, I am using a font resource(TTF file), which i load using
InputStream in = getClass().getResourceAsStream("ARIALUNI.TTF");
Font font = Font.createFont(Font.TRUE_TYPE,in);
Every time, I use this resource, a temp file named "+~JFxxxx.tmp" is created in the TEMP directory. This happens even when the app is deployed as a jar. Can anyone let me know, if this can be avoided or how to remove these temp files when the program terminates.
Thanks in advance
Pradeep
# 1
You can try to call in.close();
but, according to the documentation http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#close(), The close method of InputStream does nothing !!.
Suggestion: just before the program terminates, remove every file with name that matchs ~JFxxx.tmp pattern.
Example:
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
private static final String REGEX = "~JF(\\d*).tmp";
public static void deleteTmpFiles(){
String[] fileArray = {"test.exe", "~JF1245.tmp", "abc.jar", "~JF24.tmp"};//ONLY FOR TEST, USE REAL FILES INSTEAD
Pattern p = Pattern.compile(REGEX);
Matcher matcher;
for (int i = 0; i < fileArray.length; i++) {
matcher = p.matcher(fileArray[i]);
if(matcher.matches()){
//System.out.println(" found: "+fileArray[i]);
try {
File f = new File(fileArray[i]);
f.delete();
//f.deleteOnExit();//SEE JAVA DOCUMENTATION TO DECIDE WHETHER TO USE THIS
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
# 2
public static void deleteTmpFiles() {
Pattern p = Pattern.compile(REGEX);
Matcher matcher;
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
if (tmpDir.exists() && tmpDir.isDirectory()) {
File[] tmpFiles = tmpDir.listFiles();
for (int i = 0; i < tmpFiles.length; i++) {
if (tmpFiles[i].isFile()) {
matcher = p.matcher(tmpFiles[i].getName());
if (matcher.matches()) {
try {
System.out.println("Deleting " + tmpFiles[i].getName() + "...");
tmpFiles[i].delete();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
}
}
}
# 3
I tried the code given here, and it doesn't work. Apparently, there is no permission for the program to delete the temp file that it creates. I tried deleting the file manually while the program was running and I too got the "Access Denied" popup. I also tried the deleteOnExit() method, but that does not work either.
However, the program does delete the temp files that it had created during previous runs. It is only the most recent file that it is unable to delete due the fact that the file does not seem to be deletable untill the program has exited.
Is there anyway to get around this? I hate to leave the file in the temp directory, as it is quite a large file(22 mb).
Thanks a lot in advance!
# 4
you should give your java program access rights !!create a .bat file that launches your java program then deletes the temp files.
# 5
The problem doesn't seem to have to do with access privileges. I am unable to delete the file even manually while the program is running. The program is able to delete temp files created during previous runs of the program.
The problem seems to be that the file is undeletable until the resource that created the file is somehow formally released.
Any ideas?
Thanks again.
# 6
see my previous post !!create a second jar that only removes temp files and create a .bat (.sh under Unix)file that launches the first program then the cleaner program