Can't read multiple objects from a file - java

I'm trying to put the (Lieu) objects into an ArrayList but but at the end of the code, my list is still empty. I've been looking on the net for an answer but all I find is "Write your objects in a collection then read the collection". But the file is already written and i need to find a way to put all the (Lieu) objects in a ArrayList.
Here's the writing Code (I can't modify it):
public static void main(String[] args) {
Lieu<Double, String> p1;
Lieu<Double, String> p2;
Lieu<Double, String> p3;
SegmentGeo<String> e1;
SegmentGeo<String> e2;
SegmentGeo<String> e3;
Parcelle<String> p = new Parcelle<String>();
ArrayList<Mesure<Point<Double>, String>> segs;
p1 = new Lieu<Double, String>(45.573715, -73.900295, "p1");
p2 = new Lieu<Double, String>(45.573882, -73.899748, "p2");
p3 = new Lieu<Double, String>(45.574438, -73.900099, "p3");
e1 = new SegmentGeo<String>(p1, p2, "Parcelle test");
e2 = new SegmentGeo<String>(p2, p3, "Parcelle test");
e3 = new SegmentGeo<String>(p3, p1, "Parcelle test");
segs = new ArrayList<Mesure<Point<Double>, String>>();
segs.add(e1);
segs.add(e2);
segs.add(e3);
try {
p.setMesures(segs);
} catch (TrajectoireNonValideException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ObjectOutputStream ois = null;
try {
ois = new ObjectOutputStream(new FileOutputStream("essai.txt"));
ois.writeObject(p.informationCumulee());
ois.writeObject(p1);
ois.writeObject(p2);
ois.writeObject(p3);
} catch (EOFException ex) {
System.out.println("Fin de fichier atteinte.");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
And here's what I'm trying to do:
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
int retour = chooser.showOpenDialog(getParent());
if(retour==JFileChooser.APPROVE_OPTION){
try{
FileInputStream fis = new FileInputStream(chooser.getSelectedFile().getName());
ObjectInputStream ois = new ObjectInputStream(fis);
champNom.setText((String) ois.readObject());//that's just to display the name
while (ois.available()!=0)
{
temp = (Lieu)ois.readObject();
l.add(temp);
}
ois.close();
System.out.print(l.size());//The size is 0
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

As Joetjah says, available() doesn't work like it sounds like.
One solution that is not super elegant but works surprisingly well is to just catch the Exceptions which will be thrown when there is nothing left to read or another exception, as such:
try {
while (true)
l.add((Lieu<Double,String>)ois.readObject());
} catch (ClassNotFoundException | IOException e) {
//Expecting a EOFException here
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Whenever there's an exception is thrown when reading (and at some point there will be one!), it will stop reading.

Available doesn't do what you think it does
available() does not return the amount of data left to be read, but the amount of data that can be read without blocking (pausing to wait for more data from the file/socket/database/etc.). In some cases this may return zero while there are still bytes that should be read - the 0 means that there are 0 bytes available right now (with no blocking). This may happen for various reasons - a hard drive may be busy repositioning its magnetic reader, or a network connection may be busy, or perhaps you're waiting for a user somewhere to type something before their information may be sent. Or it may be because the file you're reading really has no additional bytes to read, because you've reached the end. Using available() you have no way of knowing whether or not you should try to read the bytes anyway.
A more correct way to use a stream to copy a file is to check the return value of read for the end-of-file value (-1):
InputStream is = // some input
OutputStream os = // some output
byte buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
When this code completes, you know that all the bytes really have been read and copied, because the while loop doesn't complete until read() returns -1, indicating the end of input.
Now, in your case, I'd advice to take it to some other direction, like this:
FileInputStream fis = new FileInputStream(chooser.getSelectedFile().getName());
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
while (obj != null)
{
champNom.setText((String)obj);
if (obj instanceof Lieu<Double, String>)
l.add(obj);
obj = ois.readObject();
}
ois.close();
System.out.print(l.size());

Related

Appendable Binary File

I'm working on appending objects to a binary file. My file is:
File f=new File("person.dat");
I'm getting an error (java.io.StreamCorruptedException: invalid stream header: 79737200) when I attempt to open the binary file. As far as I can tell the program writes the data just fine, but as soon as I try reading from it, I get the above error. Any help is appreciated!
My Code to write:
AppendObjectOutputStream out = null;
try {
out = new AppendObjectOutputStream(new FileOutputStream(f, true));
out.writeObject(new Student(name, age));
out.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
out.close();
}
My class for making appendable:
public class AppendObjectOutputStream extends ObjectOutputStream {
public AppendObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
#Override
protected void writeStreamHeader() throws IOException {
reset();
}
}
My partial code for reading and adding objects to an ArrayList:
Course course = new Course();
Student st = null;
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream("person.dat"));
try
{
while (true)
{
st = (Student) in.readObject();
course.addAccount(st); //adds student object to an ArrayList in
//class Course
}
}
catch (EOFException ex) {
}
}
catch (Exception ex) {
ex.printStackTrace();
} finally {
in.close();
}
UPDATE:
Current code to read but its not printing anything to screen:
try(ObjectInputStream ois = new ObjectInputStream(
new BufferedInputStream(Files.newInputStream(f))))
{
while (ois.available() > 0)
{
st = (Student) ois.readObject();
studentlist.addAccount(st);
System.out.println(st.getStudentNumber());
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
This is how I write to file:
Path f = Paths.get("person.dat");
try (ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(Files.newOutputStream(f, StandardOpenOption.APPEND))))
{
oos.writeObject(new Student(name,age));
}
catch(Exception ex)
{
ex.printStackTrace();
}
Rather than trying to fix your utility classes, I suggest to use the standard classes of the NIO.2 File API.
Try something like (untested):
Path personDataFilePath = Paths.get("person.dat");
// or Java 11:
// Path personDataFilePath = Path.of("person.dat");
try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(Files.newOutputStream(personDataFilePath, StandardOpenOption.APPEND)))){
oos.writeObject(new Student(name,age));
} catch (IOException ex) {
// do some error handling here
}
and to read the file, something like (untested):
List<Student> students = new ArrayList<>();
try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(Files.newInputStream(personDataFilePath)))){
while (ois.available() > 0){
students.add((Student) ois.readObject());
}
} catch (IOException ex) {
// do some error handling here
}
I have modified the code to work around making a file "appendable". I write a single arraylist object to the file (the arraylist holds a list of student objects). When I want to add a student, I read the object (arraylist) from the file, add my new student to the arraylist and write the arraylist back to the file. It is now working and my file does not have an append format.

What is wrong with creating a new .ser file in this method?

I am trying to create a new .ser file to store objects if there is not one already present. When this is ran, it throws an EOFException. What exactly is an EOFException and is this method correctly written to create and read a .ser file? Thanks for any feedback.
public void readDatabase() throws IOException {
File dataFile = new File("database.ser");
// If data file does not exist, create it.
if (!dataFile.exists()) {
System.out.println("database.ser does not exist, creating one now . . .");
// if the file doesn't exists, create it
dataFile.createNewFile();
return; // No need to try to read anything from an empty file, so return.
}
ObjectInputStream objectinputstream = null;
boolean cont = true;
try {
FileInputStream streamIn = new FileInputStream(dataFile);
objectinputstream = new ObjectInputStream(streamIn);
while (cont) {
Item obj = null;
try {
obj = (Item) objectinputstream.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (obj != null)
itemList.add(obj);
else
cont = false;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectinputstream != null) {
objectinputstream.close();
}
}
}
EOFException:
java.io.EOFException
at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2758)
at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3253)
at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866)
at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:343)
at hardwarestore.HardwareStore.readDatabase(HardwareStore.java:254)
at hardwarestore.HardwareStore.<init>(HardwareStore.java:33)
at hardwarestore.MainApp.<init>(MainApp.java:24)
at hardwarestore.MainApp.main(MainApp.java:259)
EOFException stands for:
End of File Exception
This typically occurs when you try to read data from a file where the FileReader has reached the end of the file. In other words, no more data to read.
You should catch the exception and close your stream. as it indicates that you have read all the objects in the file. Referring to the answer in this question:
while (true) {
try {
// Read the next object from the stream. If there is none, the
// EOFException will be thrown.
Item obj = (Item) objectinputstream.readObject();
itemList.add(obj);
} catch (EOFException e) {
// If there are no more objects to read, return what we have.
return contactMap;
} finally {
// Close the stream.
in.close();
}
}

How to determine if a file is empty without getting EOFException

I'm trying to create a file called manager.txt and read it. If it is empty (which it is) it will call a method to add things into it but I keep getting EOFException. I know the file is empty but it's just a part of a programI'm working on. How to determine a file is empty without getting EOFException
try(ObjectOutputStream outManager = new ObjectOutputStream(new
FileOutputStream("manager.txt"))){
try(ObjectInputStream inManager = new ObjectInputStream(new
FileInputStream("manager.txt"))){
while(true){
manager.add((Manager)inManager.readObject());
if(manager.isEmpty()){
//A method to add
}
}catch(IOException e){
}
}catch (IOException e){
}
You can read from file as follows:
try (FileInputStream fis = new FileInputStream("manager.txt");
ObjectInputStream ois = new ObjectInputStream(fis);)
{
while (fis.available() > 0) {
Object obj = ois.readObject();
if (obj instanceof Manager) {
Manager manager = (Manager) obj;
System.out.println(manager);
}
}
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
Basically, what you are looking for is fis.available().

Save java object to internal memory asynchroneously?

I need to save a java object to internal memory (a high-level collection of different server responses if to be specific). Now I use this code:
public void write(Context context) {
FileOutputStream fos = null;
try {
fos = context.openFileOutput(BACKSTACK_FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) { e.printStackTrace(); }
try {
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(backStack);
} catch (IOException e) {
L.t("failed to write backstack: " + e.toString()); e.printStackTrace();
} finally {
try { if (fos != null) fos.close(); } catch (IOException e) { }
}
}
public void read(Context context) {
FileInputStream fis = null;
try {
fis = context.openFileInput(BACKSTACK_FILENAME);
} catch (FileNotFoundException e) { e.printStackTrace();}
try {
ObjectInputStream ois = new ObjectInputStream(fis);
Deque<FragmentInfo> list = (Deque<FragmentInfo>) ois.readObject();
L.t(list.toString());
backStack = list;
} catch (IOException | ClassNotFoundException e) {
L.t("failed to read backstack" + e.toString()); e.printStackTrace();
} finally {
try { if (fis != null) fis.close(); } catch (IOException e) { }
}
}
Since we have context here, UI thread hangs(lags) white operation is performed.
And the larger object becomes, the worse it looks. So the question is:
Is there any way to save java object to internal memory asynchroneously?
Please don't recommend to use a database for such a simple task.
Saving to internal memory won't help. The real problem is that you are doing too much work on the event listener thread. You will have the same problem if you save lots of stuff to a file, a database, "internal memory" .... or to anything else. Serialization is relatively expensive no matter how you do it, and no matter where you save the results of the serialization.
The solution is to do the work using an AsyncTask.

reading objects from file

I have a problem with reading objects from file Java.
file is anarraylist<projet>
This is the code of saving objects :
try {
FileOutputStream fileOut = new FileOutputStream("les projets.txt", true);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for (projet a : file) {
out.writeObject(a);
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
And this is the code of reading objects from file ::
try {
FileInputStream fileIn = new FileInputStream("les projets.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
while (in.available() > 0){
projet c = (projet) in.readObject();
b.add(c);
}
choisir = new JList(b.toArray());
in.close();
} catch (Exception e) {
e.printStackTrace();
}
Writing is working properly. The problem is the reading... it does not read any object (projet) What could be the problem?
As mentioned by EJP in comment and this SO post . if you are planning to write multiple objects in a single file you should write custom ObjectOutputStream , because the while writing second or nth object header information the file will get corrupt.
As suggested by EJP write as ArrayList , since ArrayList is already Serializable you should not have issue. as
out.writeObject(file) and read it back as ArrayList b = (ArrayList) in.readObject();
for some reason if you cant write it as ArrayList. create custome ObjectOutStream as
class MyObjectOutputStream extends ObjectOutputStream {
public MyObjectOutputStream(OutputStream os) throws IOException {
super(os);
}
#Override
protected void writeStreamHeader() {}
}
and change your writeObject as
try {
FileOutputStream fileOut= new FileOutputStream("les_projets.txt",true);
MyObjectOutputStream out = new MyObjectOutputStream(fileOut );
for (projet a : file) {
out.writeObject(a);
}
out.close();
}
catch(Exception e)
{e.printStackTrace();
}
and change your readObject as
ObjectInputStream in = null;
try {
FileInputStream fileIn = new FileInputStream("C:\\temp\\les_projets1.txt");
in = new ObjectInputStream(fileIn );
while(true) {
try{
projet c = (projet) in.readObject();
b.add(c);
}catch(EOFException ex){
// end of file case
break;
}
}
}catch (Exception ex){
ex.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Categories

Resources