简体   繁体   中英

How to access generic user's My Downloads folder in Java?

I am writing some Selenium WebDriver and AutoIt Java Tests using Chrome. When I download a file in Chrome, it automatically downloads it to the Downloads folder on my computer. I am sharing this code project with multiple other users on a repository, and so I do not want to have my Downloads folder hard coded in with my specific username in it.

What I mean by this is let's say the user name on my computer is "Eamon." The downloads folder on my machine would be "C:\\Users\\Eamon\\Downloads," but lets say my friend Mark downloads the project from the shared repository, his downloads folder will be located at "C:\\Users\\Mark\\Downloads," yet as he pulls from the repository to see my updated code, the download location will still be hard coded as ""C:\\Users\\Eamon\\Downloads" which will result in a "Folder does not exist" error.

Is there a way in java to access the generic Downloads folder on a machine that would change based on who the user of the machine is? This would help my test a lot.

For a Windows machine :

new File("C:/Users/" + System.getProperty("user.name") + "/Downloads/");

Could use similar snippets for any other operating system.

String myHomePath= System.getProperty("user.home");

File file = new File(myHomePath+"/Downloads/" + fileName);

Note: give file name with proper extension eg foo.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM