这是我的代码
File outFile = null;
System.out.println("###DEB name = " + name);
System.out.println("###DEB extension = " + rozszezenie);
System.out.println("###DEB dir = " + dir);
if(name.length() < 3)
name = FILE_NAME_PREFIX;
outFile = File.createTempFile(name+"_", "." + rozszezenie, dir);
System.out.println("###DEB outFile = " + outFile.getName());
运行后给出输出:
###DEB name = BRAK;20160205;1016;501200911;tczelu
###DEB extension = ogg
###DEB dir = /tmp/tower_wind_tmp
###DEB outFile = BRAK;20160205;1016;501200911;tczelu_2859671049642865591.ogg
这些数字 (2859671049642865591) 从哪里来?
最佳答案
来自 createTempFile
的 Javadoc (强调我的):
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
这意味着此方法将生成一个名称,并且您无法提供自己的名称。
如果您想创建一个具有您自己名称的文件,那么您可能不想创建一个临时文件。相反,您应该使用 Files.createFile(path)
创建一个新文件。来自Java NIO.2 API ,或File#createNewFile()
否则。
关于java - Java中的临时文件名称自动更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35656743/