-1

I have used this code to create an empty textfile but it only gets stored in the project folder. I want to know how to make the text file appear on desktop?

public void newFileCreator(String filename){
    File ob = new File(filename + ".txt");
    boolean filecreated = false;
    try{
        filecreated = ob.createNewFile();
    }
    catch(IOException e){
        System.out.println("Error" + e);
    }

    if(filecreated = true){
        System.out.println("Created empty file" + ob.getPath());
    }
    else{
        System.out.println("Failed to create empty file" + ob.getPath());
    }
}
rert588
  • 737
  • 1
  • 7
  • 19
  • Give path of your desktop when you are creating new File("path" +".txt") – Gautam Savaliya Oct 10 '14 at 08:03
  • You need to specify where to put the file in "new File("/home/User/Desktop/"+filename + ".txt") – Adz Oct 10 '14 at 08:03
  • Is this Windows, Linux, Osx? – Leon Oct 10 '14 at 08:03
  • Why the downvotes? The question isn't bad. – Yann Oct 10 '14 at 08:06
  • @Yann4 Hover the downvote arrow to see possible reasons. I guess `no research effort` is the top scorer here. – Tom Oct 10 '14 at 08:11
  • @Tom Yeah, I know the possible reasons to downvote, it's just that this question seemed clear enough. I suppose that there might not have been much effort, but equally, I can see why the OP might not consider the desktop "just another folder" – Yann Oct 10 '14 at 08:13
  • Guys I tried entering the path name like most people say on the internet but it just changes the file name and has no effect on the path and still remains in the project folder. – rert588 Oct 10 '14 at 08:15

3 Answers3

2

You should change the filename to be a path to the user's desktop. You can use this question to find out how to get the desktop path. When you have that just change this line:

File ob = new File(desktop + filename + ".txt");
Community
  • 1
  • 1
MrHug
  • 1,315
  • 10
  • 27
  • I changed the file path before like how said but this code just changes the file name and still places it in the project folder. – rert588 Oct 10 '14 at 08:13
  • @rert588 Are you putting in '/' or '//'? Put the path in a different string and break to inspect it. – Yann Oct 10 '14 at 08:15
  • "//" but it also is now giving IOException Access denied. How do I resolve this. – rert588 Oct 10 '14 at 08:18
  • @rert588 try running your program as admin? – Yann Oct 10 '14 at 08:19
  • How does that help me If I am giving my project as a school project. – rert588 Oct 10 '14 at 08:20
  • @rert588 It's a hunch on what will make it run as you like. If you're looking for a way around it, that's a different question. And it's one that I'm not sure is possible without making a virus, as writing to locations that you don't have permission to write in is their thing. – Yann Oct 10 '14 at 08:24
1

You provide it the directory path to the desktop, but that depends on the system. For example, on my Linux machine, I can do ~/Desktop. When you do this, don't forget to use a // to escape the /

Yann
  • 978
  • 2
  • 12
  • 31
1

This should work. You have to define the correct path to your desktop. For instance for me it's : C:\Users\Tsou\Desktop\

public static void newFileCreator(String filename){
    File ob = new File("**C:\\Users\\Tsou\\Desktop\\**"+filename + ".txt");
    boolean filecreated = false;
    ...
}
cozla
  • 137
  • 1
  • 11