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

后端 未结 24 1096
不知归路
不知归路 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:48

    In Java 7 you can do this:

    String content = "Hello File!";
    String path = "C:/a.txt";
    Files.write( Paths.get(path), content.getBytes());
    

    There is more info here: http://www.drdobbs.com/jvm/java-se-7-new-file-io/231600403

提交回复
热议问题