File truncate operation in Java

后端 未结 7 1729
清酒与你
清酒与你 2020-11-29 11:02

What is the best-practice way to truncate a file in Java? For example this dummy function, just as an example to clarify the intent:

void readAndTruncate(Fil         


        
7条回答
  •  自闭症患者
    2020-11-29 11:40

    One liner using Files.write()...

    Files.write(outFile, new byte[0], StandardOpenOption.TRUNCATE_EXISTING);
    

    Can use File.toPath() to convert from File to Path prior as well.

    Also allows other StandardOpenOptions.

提交回复
热议问题