Question

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);
        }   
    }
Was it helpful?

Solution 2

Try printing this on the console:

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

That will tell you for sure.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top