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

后端 未结 6 728
旧巷少年郎
旧巷少年郎 2020-12-21 17:57

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

相关标签:
6条回答
  • 2020-12-21 18:22

    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.

    0 讨论(0)
  • 2020-12-21 18:23

    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;
    
    0 讨论(0)
  • 2020-12-21 18:25

    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.

    0 讨论(0)
  • 2020-12-21 18:38

    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.

    0 讨论(0)
  • 2020-12-21 18:43

    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.

    0 讨论(0)
  • 2020-12-21 18:44

    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());
    
    0 讨论(0)
提交回复
热议问题