Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Desktop path for file output Java [duplicate]

Tags:

java

desktop

I have this program where it will output a text file and save it in the user's computer, I wanted to save it at desktop since that's the path everyone would have.

I'm currently coding in Windows 8, which path should I use to guarantee it'll save to desktop on Windows 7??

File file = new File("C:/Users/Wil/Downloads/Dropbox/abc.txt");

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.close();

        JOptionPane.showMessageDialog(null,"Receipt Saved!");
like image 451
user3177001 Avatar asked Jan 11 '23 12:01

user3177001


1 Answers

File desktop = new File(System.getProperty("user.home"), "Desktop");
like image 164
user1977351 Avatar answered Jan 22 '23 15:01

user1977351