Java createTempFile 时系统找不到路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18122063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The system cannot find the path while createTempFile
提问by user1985273
I get an exception saying 'the system cannot find the path specified' while calling java function createTempFile("test", "test")
.
Tried googling but no luck.
Does anyone know from where java gets its default temp path and how can it be not found?
Windows variables seem to be correct and changing them does not affect java.
我在调用 java 函数时收到一个异常,说“系统找不到指定的路径” createTempFile("test", "test")
。尝试谷歌搜索但没有运气。有谁知道java从哪里获得它的默认临时路径以及怎么找不到它?Windows 变量似乎是正确的,更改它们不会影响 java。
采纳答案by Andreas Fester
Does anyone know from where java gets its default temp path
有谁知道 java 从哪里获得它的默认临时路径
It is read from the java.io.tmpdir
property.
它是从java.io.tmpdir
属性中读取的。
Files.createTempFile("test", "test");
essentially calls java.nio.file.TempFileHelper.createTempFile(null, prefix, suffix, attrs);
which again calls java.nio.file.TempFileHelper.create(dir, prefix, suffix, false, attrs);
. There, if dir
is null, it is set to tmpdir
which is declared as follows:
本质上调用java.nio.file.TempFileHelper.createTempFile(null, prefix, suffix, attrs);
which 再次调用java.nio.file.TempFileHelper.create(dir, prefix, suffix, false, attrs);
. 在那里,如果dir
为空,则将其设置tmpdir
为以下声明:
private static final Path tmpdir =
Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir")));
You can set the property explicitly as shown in the answer from @Joni. If you do not set it explicitly, the JVM initializes it to a platform specific default value at startup - see also Environment variable to control java.io.tmpdir?
您可以显式设置属性,如@Joni 的回答所示。如果您没有明确设置它,JVM 会在启动时将其初始化为特定于平台的默认值 - 另请参阅控制 java.io.tmpdir 的环境变量?
and how can it be not found?
怎么会找不到呢?
If the property java.io.tmpdir
points to an invalid directory, the temporary file can not be created.
如果该属性java.io.tmpdir
指向无效目录,则无法创建临时文件。
回答by Joni
Independently of how the default value is obtained, you can set the temporary files directory by setting the system property java.io.tmpdir
when starting the JVM:
独立于默认值的获取方式,您可以通过java.io.tmpdir
在启动 JVM 时设置系统属性来设置临时文件目录:
java -Djava.io.tmpdir=/path/to/where/ever/you/like YourClass
If you want to know where the default value comes from, you'll have to read the source code for your JVM. For example, OpenJDK on Windows calls the API function GetTempPathW
(search for the file java_props_md.c
in the JDK source code), which looks up the path in environment variables and registry in the following way:
如果您想知道默认值的来源,则必须阅读 JVM 的源代码。例如,Windows 上的 OpenJDK 调用 API 函数GetTempPathW
(java_props_md.c
在 JDK 源代码中搜索文件),通过以下方式在环境变量和注册表中查找路径:
The
GetTempPath
function checks for the existence of environment variables in the following order and uses the first path found:
- The path specified by the TMP environment variable.
- The path specified by the TEMP environment variable.
- The path specified by the USERPROFILE environment variable.
- The Windows directory.
Note that the function does not verify that the path exists, nor does it test to see if the current process has any kind of access rights to the path.
该
GetTempPath
函数按以下顺序检查环境变量是否存在,并使用找到的第一个路径:
- TMP 环境变量指定的路径。
- 由 TEMP 环境变量指定的路径。
- USERPROFILE 环境变量指定的路径。
- Windows 目录。
请注意,该函数不会验证路径是否存在,也不会测试当前进程是否对该路径具有任何类型的访问权限。
回答by Rahul Kulhari
Try:
尝试:
String path = System.getProperty("java.io.tmpdir");
See: get property method
参见:获取属性方法
And to add it here for completeness sake, there's also the methods createTempFile(String prefix,String suffix)and createTempFile(String prefix, String suffix, File directory)methods from Java's fileclass.
为了完整起见,还有方法createTempFile(String prefix,String suffix)和createTempFile(String prefix, String suffix, File directory)方法来自 Java 的文件类。
Here is my code to fin path of temp file and find temp path:
这是我的代码来确定临时文件的路径并找到临时路径:
public class GetTempFilePathExample
{
public static void main(String[] args)
{
try{
//create a temp file
File temp = File.createTempFile("temp-file-name", ".tmp");
System.out.println("Temp file : " + temp.getAbsolutePath());
//Get tempropary file path
String absolutePath = temp.getAbsolutePath();
String tempFilePath = absolutePath.
substring(0,absolutePath.lastIndexOf(File.separator));
System.out.println("Temp file path : " + tempFilePath);
}catch(IOException e){
e.printStackTrace();
}
}
}
Output of this code is :
此代码的输出是:
Temp file : /tmp/temp-file-name3697762749201044262.tmp
Temp file path : /tmp