Java 下载文件夹的一般路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30416365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:35:25  来源:igfitidea点击:

General Path To Downloads Folder

javafileunixdownload

提问by user3243242

I have a DownloadTask class in java that takes a filename and URL, downloads that file and saves it to the downloads folder.

我在 Java 中有一个 DownloadTask 类,它采用文件名和 URL,下载该文件并将其保存到下载文件夹中。

To save it to mydownloads folder, I have the line

要将其保存到我的下载文件夹中,我有一行

File file = new File("/users/myName/Downloads" + fileName + ".txt");

File file = new File("/users/myName/Downloads" + fileName + ".txt");

What can I replace this path with so that anyone can run the program and the file will be saved to theirdownloads folder?

我可以用什么替换此路径,以便任何人都可以运行该程序并将文件保存到他们的下载文件夹中?

采纳答案by Harsh Poddar

Check out this question. Use...

看看这个问题。用...

String home = System.getProperty("user.home");
File file = new File(home+"/Downloads/" + fileName + ".txt"); 

回答by rghome

Your own folder is accessible using the $HOME environment variable.

您可以使用 $HOME 环境变量访问您自己的文件夹。

In Java, you can find you home folder using the user.homesystem property. See Java system properties.

在 Java 中,您可以使用user.home系统属性找到您的主文件夹。请参阅Java 系统属性

e.g.:

例如:

System.getProperty("user.home");