4

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!");
user3177001
  • 45
  • 1
  • 2
  • 5

1 Answers1

9
File desktop = new File(System.getProperty("user.home"), "Desktop");
user1977351
  • 277
  • 1
  • 2
  • 9
  • How can I merge mine with yours??@user1977 mine `String content = (jLabel1.getText() + "\r\n" + N1.getText() + "\r\n" + L1.getText()); ` `File file = new File("C:/Users/Wil/Downloads/Dropbox/abc.txt");` – user3177001 Jan 09 '14 at 14:32
  • Does this work for mac? –  Feb 01 '15 at 13:13