我使用File.createTempFile()方法创建临时文件,但作为输出,它还附加了带有文件名的垃圾值。我使用上传zipfile的方法,但无法删除那些附加的垃圾值。为了进一步的功能,我需要文件的确切名称。
请帮助......
非常感谢您的回复。 我担心的是,正如niiraj874u所述的代码,我得到了文件名:tmp4501156806082176909.txt 但我只想要tmp.txt如何删除附加的数值?
答案 0 :(得分:0)
您可以使用java.io.File.getName()
方法获取文件名称。
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File f = null;
// creates temporary file
try {
f = File.createTempFile("tmp", ".txt", new File("D:/"));
} catch (IOException e) {
e.printStackTrace();
}
// prints name of temp file
System.out.println("File name: "+f.getName());
// prints absolute path
System.out.println("File path: "+f.getAbsolutePath());
}
}
这将打印出像
文件名:tmp4501156806082176909.txt
文件路径:D:\ tmp4501156806082176909.txt
答案 1 :(得分:0)
听起来你不需要临时文件。 “垃圾”的目的是保护应用程序的两个或多个实例不会相互覆盖。在这种情况下,使用system.getProperty(“java.io.temp”)来获取临时目录。