Create whole path automatically when writing to a new file

前端 未结 5 1084
野性不改
野性不改 2020-11-28 01:26

I want to write a new file with the FileWriter. I use it like this:

FileWriter newJsp = new FileWriter(\"C:\\\\user\\Desktop\\dir1\\dir2\\filename.txt\");
         


        
5条回答
  •  半阙折子戏
    2020-11-28 01:56

    Since Java 1.7 you can use Files.createFile:

    Path pathToFile = Paths.get("/home/joe/foo/bar/myFile.txt");
    Files.createDirectories(pathToFile.getParent());
    Files.createFile(pathToFile);
    

提交回复
热议问题