How do I save a String to a text file using Java?

后端 未结 24 1059
不知归路
不知归路 2020-11-22 04:18

In Java, I have text from a text field in a String variable called \"text\".

How can I save the contents of the \"text\" variable to a file?

24条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 04:52

    private static void generateFile(String stringToWrite, String outputFile) {
        try {       
            FileWriter writer = new FileWriter(outputFile);
            writer.append(stringToWrite);
            writer.flush();
            writer.close();
            log.debug("New File is generated ==>"+outputFile);
        } catch (Exception exp) {
            log.error("Exception in generateFile ", exp);
        }
    }
    

提交回复
热议问题