0

Possible Duplicate:
In java under Windows, how do I find a redirected Desktop folder?
How to get the Desktop path in java

I want to write my results to the desktop of the user rather than to the same directory as file class that I am running.

I am using Mac OS.. How about in Window?1

Thanks

Community
  • 1
  • 1
Mohamad Ibrahim
  • 5,085
  • 9
  • 31
  • 45
  • possible duplicate of [In java under Windows, how do I find a redirected Desktop folder?](http://stackoverflow.com/questions/570401/in-java-under-windows-how-do-i-find-a-redirected-desktop-folder) or [How to get the Desktop path in java](http://stackoverflow.com/questions/1080634/how-to-get-the-desktop-path-in-java) – Thomas Owens Dec 02 '11 at 13:34
  • Have you tried to write it to the desktop? Did you get any errors? We need more details really – Bali C Dec 02 '11 at 13:34
  • Please have a look at http://stackoverflow.com/questions/570401/in-java-under-windows-how-do-i-find-a-redirected-desktop-folder/570536#570536 – micfra Dec 02 '11 at 13:34
  • Related also to: http://stackoverflow.com/questions/1080634/how-to-get-the-desktop-path-in-java – Gray Dec 02 '11 at 13:35
  • @BaliC this is the point I do not know how to write to desktop assume I want to write this file bw=new BufferedWriter(new FileWriter(outFileStr)); – Mohamad Ibrahim Dec 02 '11 at 13:38
  • 1
    @ThomasOwens neither of these provide an answer actually. FileSystemView.getFileSystemView().getHomeDirectory() _will not_ give you your Desktop path – Oleg Mikheev Dec 02 '11 at 13:39
  • 1
    @Oleg I never said either of those questions had the correct answer. However, they are asking the same thing. If an answer in either of those questions is incorrect, you should comment on it explaining why, downvote it, upvote a correct answer in the question, and/or provide a correct answer. – Thomas Owens Dec 02 '11 at 13:40

1 Answers1

6

The user's home directory is:

System.getProperty("user.home")

In general +"/Desktop" would do, but is not portable.

String userHomeFolder = System.getProperty("user.home");
File textFile = new File(userHomeFolder, "mytext.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
try {
    ...
} finally {
   out.close();
}

This would write the file "mytext.txt" to the home directory.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138