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


In Java, I have text from the text field in the string variable "text".

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

If you simply do the output text, instead of any binary data, the following will work:

  PrintWriter out = new print wall ("filename.txt"); After that, type your own string, such as for any output stream:  
  out.println (text);  

You will need to deal with the exception, as ever. Be sure to call out.close () .

If you are using Java 7 or later, you can " printstream when you are done with it (i.e. exit the block) such as: < / P>

  Try (PrintWriter out = new print weter ("filename.txt")} {out .println (text);}  

You already have < Code> java.io.FileNotFoundException .


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -