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");
Share:
71,453

Related videos on Youtube

ajspencer
Author by

ajspencer

Student at Princeton

Updated on September 02, 2020

Comments

  • ajspencer
    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?

  • Zymox
    Zymox over 3 years
    This 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
    Adam Gawne-Cain almost 3 years
    Composing 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);