write an object java in a file

Solutions on MaxInterview for write an object java in a file by the best coders in the world

showing results for - "write an object java in a file"
Raphael
04 Jan 2018
1package com.mkyong;
2
3package com.mkyong;
4
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileNotFoundException;
8import java.io.FileOutputStream;
9import java.io.IOException;
10import java.io.ObjectInputStream;
11import java.io.ObjectOutputStream;
12
13public class WriterReader {
14
15	public static void main(String[] args) {
16
17		Person p1 = new Person("John", 30, "Male");
18		Person p2 = new Person("Rachel", 25, "Female");
19
20		try {
21			FileOutputStream f = new FileOutputStream(new File("myObjects.txt"));
22			ObjectOutputStream o = new ObjectOutputStream(f);
23
24			// Write objects to file
25			o.writeObject(p1);
26			o.writeObject(p2);
27
28			o.close();
29			f.close();
30
31			FileInputStream fi = new FileInputStream(new File("myObjects.txt"));
32			ObjectInputStream oi = new ObjectInputStream(fi);
33
34			// Read objects
35			Person pr1 = (Person) oi.readObject();
36			Person pr2 = (Person) oi.readObject();
37
38			System.out.println(pr1.toString());
39			System.out.println(pr2.toString());
40
41			oi.close();
42			fi.close();
43
44		} catch (FileNotFoundException e) {
45			System.out.println("File not found");
46		} catch (IOException e) {
47			System.out.println("Error initializing stream");
48		} catch (ClassNotFoundException e) {
49			// TODO Auto-generated catch block
50			e.printStackTrace();
51		}
52
53	}
54
55}
queries leading to this page
how to write an objectwrite object to file javaread and write object to file in javahow to wright java class to filewrite an object data to a file javajava serialize an object toa filejava store object in filejava objectoutputstream to filewwrite object to file systemwrite object to text file javaobject files in javajava saving class to filehow to write twho object in a file javasaving objects to a text filejava save object as array to filejava 11 saving java object to filehow to write objects to a file in javasave java object to filewrite a file object javafileobject savefile object to write javahow can we create object of file in javawrite java code to read and write object to a file write objects in a java fileobject to text filewrite java object into a filesave java object in filewrite and retrieve object data to filehow to write object into file in java how to write read write object into file using serializable in javaserialization files javasave object to file javajava save class to filewrite an object java in a filehow do you create an object saving it to an output file in javaobjects write file javawhich of the following objects can be used to write to a file in java 3fwriting objects to a file javaseralizating the object write to filejava saving objects to filesave object in javawriting objects to a file in javawriting obects to a text filewriting objects to file in javahow to write an object to a file in javaserialize object to file javahow to store java object in filejava object to filecreating a file object in javawrite into file object in javawrite an array object to file androidstore object in file javacfile object javacreate an object and write them into a fileconvert object to filehow to write java object into filewrite java objects to filejava read and write objects to fileshow to store object into file in javajava output objec to filejava serialize object tofilewriting object in file in javawriting object to file javahow to write an object write a java object to filehow to create object file in javasave class to filehow to save an object to a text file in javawrite an object to a file javajava dump object to filejava save and retrieve an object to a fileobject to text file in javawrite serialized object to file javahow to save an object to a file in javajava save object array in filehow do i serialize an object to a file 3fread and write objects to file javahow to save and read objects into a file in javajava serial object in filescast object to fiulewrite to file object javahow to make file object javajava save object as filewrite information about object in file javajava how to save data into objectsjava save object to filehow to save object into file javawriting an object in a file in javajava add object to fileserialize java object to text filesaving an object in serialization javajava serialization to filewrite java object to fileserialized object array writing in file javawrite in to serelize file in javaobject file writing in javaread and write object to file in java with outputfile handling with serializable in javahow to write object to file in javajava class to filejava writing object to filehot to store data from object to file javahow to write objects to a text file in javasave and load objects to file javawrite objects to a file in javasave object in file javawrite object data to file javasaving an object in serialization javjava write object to binary fileobject file javajava object filesprograms to read and write object in file in javawrite a program to read and write an object to file write objects to file javajava save object reading and writing objects to file in javawhich two java objects can be used to write to a file 3fwrite object in file javaobject to filesave java object in filread write java object to filejava serialize object to filewrite object in a file javajava save class objectwriting object into file in javajava write object to fileread and write object to file in java with examplehow to store an object in a file in javasave java objecthow to store data from object to file javaobject to file in javaread write object to file javacan we write serialized objects to txt filewrite file object javajava write object to file serializablehow to create a file object in javawrite obj to file javawrite an object java in a file