Java object flow instance code

  • 2020-12-10 00:42:45
  • OfStack

Write the date object and vector object to a file, then read from the file and output to the screen


package objstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.chart.PieChart.Data;
/** 
 * 
 * @author Administrator 
 */
public class ObjStream {
	/** 
   * @param args the command line arguments 
   */
	public static void main(String[] args) throws IOException {
		try {
			// this 2 A sentence with FileOutputStream fout = new FileOutputStream("test.txt"); 
			// Are equivalent  
			FileOutputStream fout = null;
			File f = new File("test.txt");
			Vector v = new Vector();
			//Vector variable  
			v.add(" Not aim at success ");
			v.add(" talented ");
			v.add(" A rapidly changing ");
			fout = new FileOutputStream(f);
			ObjectOutputStream Obj = new ObjectOutputStream(fout);
			Object Oj = new Object();
			Oj = new Date();
			Obj.writeObject(Oj);
			Obj.writeObject(v);
			Obj.close();
			FileInputStream Fin = new FileInputStream(f);
			ObjectInputStream Ois = new ObjectInputStream(Fin);
			Object Oji = new Object();
			// Why is it used here 2 a OBJECT To output for instruction  
			Oji = Ois.readObject();
			// Don't have to 2 10 words only output time  
			System.out.println(Oji);
			Object Oji2 = new Object();
			Oji2 = Ois.readObject();
			System.out.println(Oji2);
		}
		catch (FileNotFoundException ex) {
			Logger.getLogger(ObjStream.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch (ClassNotFoundException ex) {
			Logger.getLogger(ObjStream.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}

Object input stream) can read the raw data and type written by the object output stream, and the persistent storage of the object can be realized with the file input output stream 1.

Results:
Fri Jul 24 11:28:01 CST 2015
[No merit, great talent, rapid change]
Successful build (total time: 2 seconds)

But test. txt file is a garbled code. The & # 63;

conclusion

That's it for the Java object flow instance code in this article, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: