2

Possible Duplicate:
Where will be create a new file in java, when path is not given?

I am trying to create a txt file in the current directory. I ran the code below and there was no error. But I couldn't find the myfile.txt anywhere. I want to create the myfile.txt in the current directory. How can I do that?

The code that I used:

public void createFile(){
        try{
            File f = new File("myfile.txt");
            if (!f.exists()){
                f.createNewFile();
                System.out.println("New file \"myfile.txt\" has been created");
            }
        }catch(Exception e){
            System.out.println("Error while creating file " + e);
        }   
    }
Community
  • 1
  • 1
kaboom
  • 833
  • 4
  • 18
  • 38

2 Answers2

4

It's in the user directory, which can be retrieved with:

 System.getProperty("user.dir");

See here for details:

In Java, what is the default location for newly created files?

Community
  • 1
  • 1
mjuarez
  • 16,372
  • 11
  • 56
  • 73
3

Try printing this on the console:

System.out.println(f.getAbsolutePath());

That will tell you for sure.

Óscar López
  • 232,561
  • 37
  • 312
  • 386