简体   繁体   中英

Why can't i see the textfile that i've created in the working directory?

File f = new File("textfile.txt");
        System.out.println(f.getAbsolutePath());

This prints out the path of the textfile, but when i go to it's directory the textfile is not there. Is the textfile hidden or not created?

Because, you didn't create a file, you created a File .

File represents a virtual/abstract filename . It doesn't need to actually exist on the disk.

While there is a few ways to do this, the simplest might be to use File#createNewFile , just make sure you check the return value, as it's not gurenteed to work

File file = new File("textfile.txt");
System.out.println("Exists: " + f.exists());
if (!file.exists()) {
    if (!file.createNewFile()) {
        System.out.println("Sorry, could not create " + file);
    } else {
        System.out.println("Create file " + file + " successfully");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM