how to delete the content of text file without deleting itself

前端 未结 17 1318
逝去的感伤
逝去的感伤 2020-11-28 03:05

I want to copy the content of file \'A\' to file \'B\'. after the copying is done I want to clear the content of file \'A\' and want to write on it from its beginning. I can

17条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 03:45

    With try-with-resources writer will be automatically closed:

    import org.apache.commons.lang3.StringUtils;
    final File file = new File("SomeFile");
    try (PrintWriter writer = new PrintWriter(file))  {
        writer.print(StringUtils.EMPTY);                
    }
    // here we can be sure that writer will be closed automatically
    

提交回复
热议问题