简体   繁体   中英

why new FileWriter(“abc.txt”) creates a new file and new File(“abc.txt”) does not?

new File("abc.txt") does not create actual file while new FileWriter("abc.txt") creates a file on disk. While going through source code i found that new FileWriter("abc.txt") eventually creates an object of file like new File()

Constructor of Class java.io.File does not create file on disk. It is just a abstraction over the file path. The file is created when you write to the file.

When you are creating FileWriter it calls constructor of FileOutputStream that calls a sequence of security checks and then invokes:

if (append) {
    openAppend(name);
} else {
    open(name);
}

Invocation of open() creates file on disk.

EDIT:

Here is how open() is defined:

/**
 * Opens a file, with the specified name, for writing.
 * @param name name of file to be opened
 */
private native void open(String name) throws FileNotFoundException;

I think file.createNewFile() creates the new file in actual.. please see following code for detal...

  File file = new File("D:\\tables\\test.sql");

            // if file does not exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());

File doesn't always need to represent an actual file, it can be something you plan on creating, are guessing at the existence of, or something you've deleted as well.

From the JavaDoc for java.io.File :

An abstract representation of file and directory pathnames.

and

Instances of this class may or may not denote an actual file-system object such as a file or a directory.

In order to have the file actually be created, one needs to call createNEwFile() , whic according to the JavaDoc:

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

The File object is simply a representation of a file's location (URL) in the system. You can call createNewFile() on a File object in order to write our a file assuming one with that name does not already exist in that location.

When FileWriter creates a new File object internally, this is not what causes the file to come into existence. That happens in other parts of the code. A File object is just a standard way to designate a file (or directory), whether or not it exists.

There are lots of reasons really, but:

File is used by many classes in java.io. FileReader, etc... FileWriter is a "convenience" class that uses File and it enables the programmer to be more productive. Some Classes just want a File object which points to a file location and then they operate on it as needed to support their processing. Other Classes might support a FileWriter because it will only be writing to a file and not reading. It also makes the API more strongly-typed.

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