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

后端 未结 6 750
旧巷少年郎
旧巷少年郎 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: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.

提交回复
热议问题