0

I have a buffered writer, How can I make it put text file in the user's desktop, currently I have to specify a location and I cannot choose it to be the user's desktop location, doesn't matter who is logged in.

private static void writingFile() {
        try {
            BufferedWriter writeArrayList = new BufferedWriter (new FileWriter("D:\\text.txt"));
            for(GetArray s : arrayList){
                writeArrayList.write(s.toString() + "\r\n");
            }
            writeArrayList.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

Thanks.

---------------------Edited----------------------------Code improved-----------

try {
        File homeDir = new File(System.getProperty("user.home"));
        File file = new File(homeDir, "Desktop");
        FileOutputStream oStream = new FileOutputStream(file);
        BufferedWriter writeArrayList = new BufferedWriter(new OutputStreamWriter(oStream));
            for(GetArray s : arrayList){
                writeArrayList.write(s.toString() + "\r\n");
            }
            writeArrayList.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
user207421
  • 305,947
  • 44
  • 307
  • 483
  • This seems to be either a duplicate of or very similar to http://stackoverflow.com/questions/570401/in-java-under-windows-how-do-i-find-a-redirected-desktop-folder – Dawood ibn Kareem Feb 06 '14 at 23:25

1 Answers1

0

Something like this maybe?

File homeDir = new File(System.getProperty("user.home"));
File file = new File(homeDir, "Desktop");
FileOutputStream oStream = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(oStream));

The Desktop is a an OS specific concept. So you have to know the address scheme of the OS. Like in OSX it's user.home/Desktop, Windows might be something like user.home/My Desktop, linux ... eh depends on if you have a X environment or not and if there is anything on top of that, like gnome, kde, etc.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • Hi, is there a one line code for this, if not would it be possible if I give user the option to select a location instead of me entering it in the java program. Thanks. – user3278130 Feb 06 '14 at 23:19
  • Yeah you can specify the file to point to whereever you want, I just spelled it out for clarity. But you asked specifically for the desktop. – Greg Giacovelli Feb 07 '14 at 00:06
  • Well, that part of your code that has `new FileWriter()` you could adapt what I am doing here, or you could just remove that line entirely put my code in and rename the `BufferedWriter` to `writeArrayList` – Greg Giacovelli Feb 07 '14 at 01:14
  • Hi, it does not work, red messages appear in the console of eclipse. – user3278130 Feb 07 '14 at 02:07
  • Red messages you say? might need a new computer. ;) So That isn't helpful. Might be worthwhile to post your code or what you have done. I can only help so much. This task requires that you have basic java knowledge and filesystem knowledge – Greg Giacovelli Feb 07 '14 at 02:49
  • Hi, sorry for not posting the code here, I have editing my question, could you please check, again I am sorry completely forgot. thanks. – user3278130 Feb 07 '14 at 06:12
  • Again what is the problem? Voting for close. This is getting sort of nuts. My only advise might be to check to see if the directory "Desktop" exists and/or create it. But basically you need to know the path of the "Desktop" for the User and Operating System scheme you are using. – Greg Giacovelli Feb 24 '14 at 21:34