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

后端 未结 24 1349
不知归路
不知归路 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条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 04:31

    It's better to close the writer/outputstream in a finally block, just in case something happen

    finally{
       if(writer != null){
         try{
            writer.flush();
            writer.close();
         }
         catch(IOException ioe){
             ioe.printStackTrace();
         }
       }
    }
    

提交回复
热议问题