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

后端 未结 6 734
旧巷少年郎
旧巷少年郎 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: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());
    

提交回复
热议问题