Dumping a Java StringBuilder to File

前端 未结 9 602
南旧
南旧 2021-02-01 14:17

What is the most efficient/elegant way to dump a StringBuilder to a text file?

You can do:

outputStream.write(stringBuilder.toString().getBytes());
         


        
9条回答
  •  醉话见心
    2021-02-01 14:35

    Since java 8 you only need to do this:

    Files.write(Paths.get("/path/to/file/file_name.extension"), stringBuilder.toString().getBytes());

    You don't need any third party libraries to do that.

提交回复
热议问题