Java write string to text file code example

1、 Filewriter and file -- write string to text file

2、 InputStream and OutputStream input and output streams

3、 Objectoutputstream and objectinputstream

Objectoutputstream writes the basic data types and graphics of Java objects to OutputStream. Objects can be read (reconstructed) using objectinputstream. Persistent storage of objects can be achieved by using files in the stream.

Writes serialized objects to a file

1. Writes serialized objects to a file

FileOutputStreamfileStream=newFileOutputStream(“Myobject.ser”);// If it does not exist, it is created automatically

2. Create objectoutputstream

ObjectOutputStreamos=newObjectOutputStream(fileStream);

3. Write object

os. writeObject(one);// One is the reference name of an object instance

4. Close objectoutputstream

os. close

Objectinputstream is used to deserialize

Deserialization

1. Create FileInputStream

FileInputStreamfileStream=newFileInputStream(“MyObject.ser”);

2. Create objectinputstream

ObjectInputStreamos=newObjectInputStream(fileStream);

3. Read object

Objectone=os. readObject();

4. Convert object type

Modelelf=(Model)one;// Model is the class name of the one object

5. Close objectinputstream

os. close();

summary

The above is all about the code example of Java writing strings to text files. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>