Dumping a Java StringBuilder to File

前端 未结 9 619
南旧
南旧 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条回答
  •  猫巷女王i
    2021-02-01 14:26

    You could use the Apache Commons IO library, which gives you FileUtils:

    FileUtils.writeStringToFile(file, stringBuilder.toString(), Charset.forName("UTF-8"))
    

提交回复
热议问题