write an object java in a file
Java
package com.mkyong;
package com.mkyong;
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;
public class WriterReader {
public static void main(String[] args) {
Person p1 = new Person("John", 30, "Male");
Person p2 = new Person("Rachel", 25, "Female");
try {
FileOutputStream f = new FileOutputStream(new File("myObjects.txt"));
ObjectOutputStream o = new ObjectOutputStream(f);
// Write objects to file
o.writeObject(p1);
o.writeObject(p2);
o.close();
f.close();
FileInputStream fi = new FileInputStream(new File("myObjects.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
// Read objects
Person pr1 = (Person) oi.readObject();
Person pr2 = (Person) oi.readObject();
System.out.println(pr1.toString());
System.out.println(pr2.toString());
oi.close();
fi.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Also in Java:
- Title
- java
- Category
- Java
- Title
- convert list of integer to array in java
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- iterate trough linked list java
- Category
- Java
- Title
- how to use the this keyword in java
- Category
- Java
- Title
- fastapi list query parameter
- Category
- Java
- Title
- java processbuilder example
- Category
- Java
- Title
- calculate days between two dates in java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- how to get orientation lock to portrait android stackoverflow
- Category
- Java
- Title
- récupérer les données d un liste java
- Category
- Java
- Title
- simple calculator program in java
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- java variable string
- Category
- Java
- Title
- java 8 iterating and manipulating list
- Category
- Java
- Title
- java get environment variables
- Category
- Java
- Title
- gradle require java version
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- java calculator code
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- java random numbers in specific range
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- change number into array in java
- Category
- Java
- Title
- on click android studio not working
- Category
- Java
- Title
- null pointer exception java
- Category
- Java
- Title
- connect 2 package in android
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- capitalize string java
- Category
- Java
- Title
- processing string to int
- Category
- Java
- Title
- number to char java
- Category
- Java
- Title
- shortcut to find a class in java project eclipse
- Category
- Java
- Title
- how to have a only number type in java
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- how to remove duplicates from an array java
- Category
- Java
- Title
- java map get the key from value
- Category
- Java
- Title
- removeeventlistener
- Category
- Java
- Title
- java mockito print called methods
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- check if optional is empty java
- Category
- Java
- Title
- file with line numbers inserted java
- Category
- Java
- Title
- kotlin android intent pass data
- Category
- Java
- Title
- transformer un string en Biginteger java
- Category
- Java
- Title
- how to define an arraylist in java
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- entre clavier java
- Category
- Java
- Title
- remove extra blank spaces from string in java
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- java get input
- Category
- Java
- Title
- print a string java
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- setting up javafx in eclipse
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- eliminar fila jtable java
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- how to scan a character in java
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- how to add java_home in mac
- Category
- Java
- Title
- add video in bootstrap
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- kotlin add value to MutableLiveData
- Category
- Java
- Title
- official java website
- Category
- Java
- Title
- get long from date java
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- convert hashset to int array java
- Category
- Java
- Title
- java hashmap get value
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- immagini java
- Category
- Java
- Title
- java swing draw centered text
- Category
- Java
- Title
- log4j print stacktrace
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- executors java
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- java loop hashmap
- Category
- Java
- Title
- change activity main drawer items text color programmatically android
- Category
- Java
- Title
- spigot heal player
- Category
- Java
- Title
- android how to split string
- Category
- Java
- Title
- count the number of words in a string java
- Category
- Java
- Title
- how to declare an array in java
- Category
- Java
- Title
- java scanner
- Category
- Java
- Title
- prendere valore da tastiera java
- Category
- Java
- Title
- java - a program to print open ports
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- double to string java
- Category
- Java