I have a program in which I am trying to implement saving and loading of obejcts, however I couldn't get the loading to work after the program closes, so effectively only saving and loading works while the program is open, but no data is ever loaded once the program starts. I assume this is something to do with overwiting. I created a test program to see if I could get it to work just using a simple Person class. I store my Peson objects inside an ArrayList and serialize it, then deserialize it. Currently I am storing all loaded Person objects in a JComboBox. I have looked online and could not find anything that will help. Also note I am aware that using serialization is not the best method of saving objects, but it's something suitable to use for my program.
My App Class:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
public class App extends JFrame {
public static JComboBox<Person> peopleBox;
public App(){
try {
Person.peopleList = loadList();
}
catch(IOException | ClassNotFoundException e){
System.out.println(e.getMessage());
}
try {
saveList(Person.peopleList);
}catch (IOException e){
System.out.println(e.getMessage());
}
peopleBox = new JComboBox<>();
peopleBox.setModel(getComboBoxModel(Person.peopleList));
add(peopleBox);
pack();
setSize(600, 400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public DefaultComboBoxModel<Person> getComboBoxModel(ArrayList<Person> peopleList){
Person[] comboBoxModel = peopleList.toArray(new Person[0]);
return new DefaultComboBoxModel<>(comboBoxModel);
}
public static void saveList(ArrayList<Person> peopleList) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.bin"));
objectOutputStream.writeObject(peopleList);
}
public static ArrayList<Person> loadList() throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.bin"));
Person.peopleList = (ArrayList<Person>) objectInputStream.readObject();
return Person.peopleList;
}
public static void main(String[] args){
// Person p = new Person("Sean", 22);
try {
saveList(Person.peopleList);
}catch (IOException e){
System.out.println(e.getMessage());
}
App app = new App();
app.pack();
app.setVisible(true);
}
}
Person Class
import java.io.Serializable;
import java.util.ArrayList;
public class Person implements Serializable {
public int age;
public String name;
public static ArrayList<Person> peopleList = new ArrayList<>();
public Person(String name, int age){
this.age = age;
this.name = name;
peopleList.add(this);
for(Person p : peopleList){
System.out.println(p.toString());
}
}
public Person(){
}
public String toString(){
return "Name : " + name + " Age: " + age;
}
}
I expect when I save the list to the "test.bin" file, close the program, then open it again that it will load the list and display the Objects I created before I closed the program. I appreciate any help, thanks.
You are saving an empty list before you load Person from the file.
I suggest this approach:
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class App extends JFrame {
public static JComboBox<Person> peopleBox;
public App() {
try {
loadList();
} catch (IOException | ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
saveList(Person.peopleList);
} catch (IOException e) {
System.out.println(e.getMessage());
}
setSize(600, 400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void updateData(){
peopleBox = new JComboBox<>();
peopleBox.setModel(getComboBoxModel(Person.peopleList));
add(peopleBox);
pack();
}
public DefaultComboBoxModel<Person> getComboBoxModel(ArrayList<Person> peopleList) {
Person[] comboBoxModel = peopleList.toArray(new Person[0]);
return new DefaultComboBoxModel<>(comboBoxModel);
}
public static void saveList(ArrayList<Person> peopleList) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.bin"));
objectOutputStream.writeObject(peopleList);
}
public static void loadList() throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test.bin"));
Person.peopleList.addAll((List<Person>) objectInputStream.readObject());
}
public static void main(String[] args) {
App app = new App();
Person p = new Person("Sean2", 24);
try {
saveList(Person.peopleList);
} catch (IOException e) {
System.out.println(e.getMessage());
}
app.updateData();
app.setVisible(true);
}
}
Related
I am trying out serializing and deserializing a java application. It's working but I would like to know if I am doing this process correctly. I am serializing the ArrayList; is this the proper way or should I be serializing the Employee class, but how would I go about that considering we could have many employees?
The way I am doing it causes this error which I would like to get rid of:
serialTest.java:40: warning: [unchecked] unchecked conversion
employees = (ArrayList) ois.readObject(); required: ArrayList found: ArrayList 1 warning
Here is the Employee class:
package serialTest;
import java.io.Serializable;
public class Employee implements Serializable {
int id;
String firstName;
String lastName;
public Employee(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
And here is the main class:
package serialTest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class serialTest {
static ArrayList<Employee> employees = new ArrayList<>();
public static void main(String[] args) {
if (args.length > 0) {
deSerialize();
} else {
employees.add(new Employee(1, "John", "Doe"));
employees.add(new Employee(2, "Jane", "Doe"));
serialize();
}
}
private static void serialize() {
System.out.println("Serializing...");
try {
try (FileOutputStream fos = new FileOutputStream("employeeData"); ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(employees);
}
} catch (IOException ioe) {
}
}
private static void deSerialize() {
System.out.println("DeSerializing...");
try {
try (FileInputStream fis = new FileInputStream("employeeData");
ObjectInputStream ois = new ObjectInputStream(fis)) {
employees = (ArrayList) ois.readObject();
}
} catch (IOException ioe) {
System.out.println("File problems");
return;
} catch (ClassNotFoundException c) {
System.out.println("Class problems");
return;
}
for (Employee info : employees) {
System.out.println(info);
}
}
}
You will have to suppress that warning. Java doesn't know the actual generic type due to type erasure.
I came up with a solution. Would appreciate some comments.
package serialTest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
public class serialTest {
static ArrayList<Employee> employees = new ArrayList<>();
public static void main(String[] args) {
if (args.length > 0) {
deSerialize();
} else {
employees.add(new Employee(1, "John", "Doe"));
employees.add(new Employee(2, "Jane", "Doe"));
serialize();
}
}
private static void serialize() {
System.out.println("Serializing...");
try {
try (FileOutputStream fos = new FileOutputStream("employeeData");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject((Employee[]) employees.toArray(new Employee[employees.size()]));
}
} catch (IOException ioe) {
}
}
private static void deSerialize() {
System.out.println("DeSerializing...");
try {
try (FileInputStream fis = new FileInputStream("employeeData");
ObjectInputStream ois = new ObjectInputStream(fis)) {
employees.addAll(Arrays.asList((Employee[]) ois.readObject()));
}
} catch (IOException ioe) {
System.out.println("File problems");
return;
} catch (ClassNotFoundException c) {
System.out.println("Class problems");
return;
}
for (Employee info : employees) {
System.out.println(info);
}
}
}
i have to do a practice in my uni, it must create a blockchain using sockets and serialization in a "simple way". But when exiting the loop (typing "NO") It creates a EOF exception that i cannot solve, while closing the socket(s.close()). i would appreciate some help, i am not vry good at java. here are my classes.
Client
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client implements Runnable{
public static void main(String[] args) throws Exception {
(new Thread(new Client())).start();
}
public static MedicalReport createReport(){
return new MedicalReport(10,"pepe","id","record");
}
#Override
public void run() {
// int port = 12345;
// String computer = "localhost";
try{
Socket s = new Socket("localhost", 12348);
ObjectOutputStream p = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
/* PrintWriter print = new PrintWriter(s.getOutputStream());
print.println("ready");
print.flush();*/
//manda informe al servidor serializado y espera respuesta
boolean stop = false;
while(!stop){
try{
MedicalReport report = createReport();
p.writeObject(report);
p.flush();
p.reset();
System.out.println("Do you want to continue? Yes or No");
Scanner in1 = new Scanner (System.in);
String answer="";
if(in1.hasNextLine())
answer = in1.nextLine();
if(!answer.equalsIgnoreCase("yes")){
System.out.println(report);
stop = true;
}
}
catch(Exception e){
System.out.println(e);
}
}
try{
s.close();
}
catch(Exception e){
System.out.println(e);
}
}catch(Exception e){
System.out.println(e);
}
}
}
SERVER
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class Server {
public static void main(String[] args)
throws Exception
{
ArrayList<Block> blockChain = new ArrayList<>();
try{
ServerSocket ss = new ServerSocket(12348);
Socket s = ss.accept();
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
/* Scanner scanner = new Scanner(s.getInputStream());
String text = scanner.nextLine();*/
int i = 0;
int previousHash = 0;
while (i != 20){
MedicalReport rp = (MedicalReport)in.readObject();
Block block = new Block(rp,previousHash);
blockChain.add(block);
System.out.println("Block " + blockChain.size() + " added to blockchain");
System.out.println(blockChain.get(i));
previousHash = block.getBlockHash();
System.out.println(blockChain);
i++;
}
try{
ss.close();
}
catch(Exception e){
System.out.println(e);
}
}catch(Exception e){
System.out.println(e);
}
}
}
It looks like the error is while closing the socket, any idea?
EDIT REST OF THE CODE
MEDICAL REPORT
import java.io.Serializable;
public class MedicalReport implements Serializable {
private int age;
private String name;
private String id;
private String record;
private static final long serialVersionUID = 1L;
public MedicalReport(){super();}
public MedicalReport(int age, String name, String id, String record) {
super();
this.age = age;
this.name = name;
this.id = id;
this.record = record;
}
public String getRecord(){
return this.record;
}
public String toString(){
return this.name + ". \n" + this.age + ". \n" + this.id + ". \n" + this.record;
}
}
BLOCK
public class Block {
private int blockHash;
private int previousHash;
private MedicalReport report;
//Block Constructor.
public Block(MedicalReport report,int previousHash ) {
this.previousHash = previousHash;
this.report = report;
this.blockHash = report.hashCode();
}
public int getPreviousHash() {
return previousHash;
}
public MedicalReport getReport() {
return report;
}
public int getBlockHash() {
return blockHash;
}
}
EDIT 2
FIRST QUESTION SOLVED. Now i get this error when exiting the loop:
java.net.SocketException: Connection reset by peer: socket write error
readObject() throws EOFEzception when the peer has closed the connection. This is normal. Catch it and stop reading. There is no problem here to solve.
IMPORTANT: As EJP said EOFException is normal and you can control the flow of your code with it but if you still want to know how to do in the way you asked here it is. REMEMBER THIS IS JUST FULFILL YOUR QUESTION AND NOT ADVISED TO DO SO.
On Server Class
Replace
MedicalReport rp = (MedicalReport)in.readObject();
With
MedicalReport rp;
if((rp = (MedicalReport)in.readObject())==null) break;
On Client Class
ADD
p.writeObject(null);
Just above the s.close(); statement
You must know that when a peer close the connection normally then
read() returns -1,
readLine() returns null,
readXXX() throws EOFException for any other XXX
And A write will throw an IOException
As of now, i am using AudioPlayer to create sound effects for my game, and I am struggling to find an easy way to reset the audio before I begin playing it again. As of now I am just reloading the AudioStream entirely, heres my code, any suggestions are welcome.
package resources;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import javax.sound.sampled.Clip;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Audio {
private static HashMap<String, AudioStream> sounds = new HashMap<String, AudioStream>();
private static HashMap<String, Long> times = new HashMap<String, Long>();
Clip c;
public static AudioStream getAudio(String s){
if(sounds.containsKey(s))
return sounds.get(s);
return null;
}
public static void loadAllAudio(){
File f = new File("res/sounds");
String[] files = f.list();
for(String s:files){
if(s.endsWith(".wav")){
loadAudio(s);
System.out.println("Loaded Audio: "+s);
}
}
}
public static double getSoundLength(String sound){
return getAudio(sound).getLength()/192000.0;
}
public static void playSound(String name){
resetSound(name);
times.put(name, System.nanoTime());
AudioPlayer.player.start(sounds.get(name));
}
public static void resetSound(String name){
loadAudio(name);
}
public static void stopSound(String sound){
AudioPlayer.player.stop(sounds.get(sound));
times.put(sound, System.nanoTime());
}
private static void loadAudio(String name){
try {
File f = new File("res/sounds/"+name);
sounds.put(name, new AudioStream(new FileInputStream(f)));
times.put(name, 0L);
} catch (Exception e) {}
}
public static double getTime(String sound) {
long time = times.get(sound);
if(time==0)
return 0;
return (System.nanoTime()-time)/1E9;
}
}
The line I would like to replace is:
public static void playSound(String name){
---------resetSound(name);--------- THIS LINE
times.put(name, System.nanoTime());
AudioPlayer.player.start(sounds.get(name));
}
You can use JLayer which supports .mp3
An example how to play sound with JLayer:
new Thread(()->{
try {
FileInputStream file = new FileInputStream("path ..../audio.mp3"); //initialize the FileInputStream
Player player= new Player(file); //initialize the player
player.play(); //start the player
} catch (Exception e) {
e.printStackTrace();
}
}).start();
Note:
Note that i am using a separate Thread cause if not the App will stack.
my research on google-search and stackoverflow regarding this problem didn't resolve it.
i'd like to show you a snipped of my Datastructure:
there's a class, called "SequenceHolder" => this one carries an:
ArrayList<SequenceData> data;
within the Sequenceholder, there is a function to call the serialization:
public void writeSequenceList() throws FileNotFoundException, IOException {
FileOutputStream fout = new FileOutputStream(path);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(data);
oout.close();
fout.close();
}
The class SequenceObject has following fields: (this one is on the top, where i start the serialization)
private ArrayList<SequenceModel> recordedSequenceData;
private String sequenceUrl;
while the SequenceModel is defined like this:
private Object sequenceRawData;
private boolean isProcessedByRequest;
The sequenceRawdata objects are basically two other classes (containing Strings only)!
every class of this "trail" implements the interface "Serializable".
this is the deserialization:
public ArrayList<SequenceData> loadSequenceList() throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fileIn = new FileInputStream(path);
ObjectInputStream in = new ObjectInputStream(fileIn);
this.data = (ArrayList<SequenceData>) in.readObject();
in.close();
fileIn.close();
return data; // load from de-serialization
}
after a deserialization of the SequenceObject, i'll only retrieve the "sequenceUrl", but no recordedSequenceData.
Is there a trick to do this?!
It came just up to my mind, to extend some classes with the ObjectOutputStream and call the writingprocess with "this" explicitly in every class - but yeah, i am not sure if thats a good idead.
What do you mean by "The sequenceRawdata objects are basically two other classes (containing Strings only)!" because when I try to run the same program :
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.io.Serializable;
import java.util.ArrayList;
class SequenceModel implements Serializable
{
public SequenceModel(Object a, boolean b)
{
sequenceRawData = a;
isProcessedByRequest = b;
}
public String toString()
{
return (String)sequenceRawData + isProcessedByRequest + " SeqModel ";
}
private Object sequenceRawData;
private boolean isProcessedByRequest;
}
class SequenceData implements Serializable
{
public SequenceData(ArrayList<SequenceModel> a, String b)
{
recordedSequenceData = a;
sequenceUrl = b;
}
public String toString()
{
return recordedSequenceData + sequenceUrl + " SeqData ";
}
private ArrayList<SequenceModel> recordedSequenceData;
private String sequenceUrl;
}
class SequenceHolder implements Serializable
{
ArrayList<SequenceData> data;
public String toString()
{
return data + " SeqHol ";
}
public SequenceHolder(ArrayList<SequenceData> a)
{
data = a;
}
public void writeSequenceList() throws FileNotFoundException, IOException {
FileOutputStream fout = new FileOutputStream(Test.file);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(data);
oout.close();
fout.close();
}
public ArrayList<SequenceData> loadSequenceList() throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fileIn = new FileInputStream(Test.file);
ObjectInputStream in = new ObjectInputStream(fileIn);
this.data = (ArrayList<SequenceData>) in.readObject();
in.close();
fileIn.close();
return data; // load from de-serialization
}
}
public class Test
{
public static File file = new File("abc.txt");
public static void main(String[] args)
{
SequenceModel obj = new SequenceModel("abc", false);
ArrayList list = new ArrayList(); list.add(obj);
SequenceData obh = new SequenceData(list, "str");
ArrayList l2 = new ArrayList();
l2.add(obh);
SequenceHolder obi = new SequenceHolder(l2);
try {
obi.writeSequenceList();
System.out.println(obi.loadSequenceList());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
it is able to serialize and deserialize both and there is no problem.
Output it is printing is : [[abcfalse SeqModel ]str SeqData ] which includes everything as desired.
Please let me know if I am missing anything.
Helo, there are 3 files, CustomerClient.java, CustomerServer.java and Customer.java
PROBLEM: In the CustomerServer.java file, i get an error when I compile the CustomerServer.java at line : System.out.println(a[k].getName());
ERROR:
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\build\classes
C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\src\CustomerServer.java:44: cannot find symbol
symbol : method getName()
location: class Customer
System.out.println(a[k].getName());
1 error
BUILD FAILED (total time: 0 seconds)
CustomerClient.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CustomerClient extends JApplet {
private JTextField jtfName = new JTextField(32);
private JTextField jtfSeatNo = new JTextField(32);
// Button for sending a student to the server
private JButton jbtRegister = new JButton("Register to the Server");
// Indicate if it runs as application
private boolean isStandAlone = false;
// Host name or ip
String host = "localhost";
public void init() {
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 1));
p1.add(new JLabel("Name"));
p1.add(jtfName);
p1.add(new JLabel("Seat No."));
p1.add(jtfSeatNo);
add(p1, BorderLayout.CENTER);
add(jbtRegister, BorderLayout.SOUTH);
// Register listener
jbtRegister.addActionListener(new ButtonListener());
// Find the IP address of the Web server
if (!isStandAlone) {
host = getCodeBase().getHost();
}
}
/** Handle button action */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
// Establish connection with the server
Socket socket = new Socket(host, 8000);
// Create an output stream to the server
ObjectOutputStream toServer =
new ObjectOutputStream(socket.getOutputStream());
// Get text field
String name = jtfName.getText().trim();
String seatNo = jtfSeatNo.getText().trim();
// Create a Student object and send to the server
Customer s = new Customer(name, seatNo);
toServer.writeObject(s);
} catch (IOException ex) {
System.err.println(ex);
}
}
}
/** Run the applet as an application */
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Register Student Client");
// Create an instance of the applet
CustomerClient applet = new CustomerClient();
applet.isStandAlone = true;
// Get host
if (args.length == 1) {
applet.host = args[0];
// Add the applet instance to the frame
}
frame.add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.setVisible(true);
}
}
CustomerServer.java
import java.io.*;
import java.net.*;
public class CustomerServer {
private String name;
private int i;
private ObjectOutputStream outputToFile;
private ObjectInputStream inputFromClient;
public static void main(String[] args) {
new CustomerServer();
}
public CustomerServer() {
Customer[] a = new Customer[30];
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
System.out.println("Server started ");
// Create an object ouput stream
outputToFile = new ObjectOutputStream(
new FileOutputStream("student.dat", true));
while (true) {
// Listen for a new connection request
Socket socket = serverSocket.accept();
// Create an input stream from the socket
inputFromClient =
new ObjectInputStream(socket.getInputStream());
// Read from input
//Object object = inputFromClient.readObject();
for (int k = 0; k <= 2; k++) {
if (a[k] == null) {
a[k] = (Customer) inputFromClient.readObject();
// Write to the file
outputToFile.writeObject(a[k]);
//System.out.println("A new student object is stored");
System.out.println(a[k].getName());
break;
}
if (k == 2) {
//fully booked
outputToFile.writeObject("All seats are booked");
break;
}
}
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
inputFromClient.close();
outputToFile.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
Customer.java
public class Customer implements java.io.Serializable {
private String name;
private String seatno;
public Customer(String name, String seatno) {
this.name = name;
this.seatno = seatno;
}
public String getName() {
return name;
}
public String getSeatNo() {
return seatno;
}
}
The build message says its only compiling one source file. Perhaps the Customer class changed to include the getName function and has not been recompiled since.
Did you try compiling all three source files at the same time?
Recompile the Customer.java and make sure you don't have duplicate versions of the class file hanging around. Use debugger (set breakpoint after the customer class de-serialization) for further debugging.