General Path To Downloads Folder
71,453
Solution 1
Check out this question. Use...
String home = System.getProperty("user.home");
File file = new File(home+"/Downloads/" + fileName + ".txt");
Solution 2
Your own folder is accessible using the $HOME environment variable.
In Java, you can find you home folder using the user.home
system property. See Java system properties.
e.g.:
System.getProperty("user.home");
Related videos on Youtube
Comments
-
ajspencer over 3 years
I have a DownloadTask class in java that takes a filename and URL, downloads that file and saves it to the downloads folder.
To save it to my downloads folder, I have the line
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 their downloads folder?
-
Erik over 3 yearsFor Linux and similar systems, see stackoverflow.com/questions/22250118/…
-
Erik over 3 yearsDoes this answer your question? How can I get system user documents, pictures, music folders independent of OS using Java?
-
-
Zymox over 3 yearsThis is actually not a reliable way to get the Downloads folder in Windows, because users can change the folder (so that it is not under their home directory anymore). See here: blog.samirhadzic.com/2018/03/01/…
-
Adam Gawne-Cain almost 3 yearsComposing a path string with "/" may not work on platforms with a different separator character. Use this instead: File f = new File(new File(home, "Downloads"), fileName + ".txt);