• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

? File I/O (and-or) for each loop

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this make sense?



Get list being in the following file.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It writes each object in the list to an ObjectOutputStream. Makes perfect sense, why?
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm catching an I/O Exception.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geoff Jefferson wrote:I'm catching an I/O Exception.



OK, so what's the error message? Exactly which line of your code does the stack trace implicate?
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I caught it here. I suppose I had better learn to output the stack trace.

try {
fOut = new FileOutputStream ("C:\\DataFiles");
oOS = new ObjectOutputStream(fOut);

for (ItemFile obj: tableModel.getList()) { // for-each loop

oOS.writeObject(obj);
}
}
catch(IOException evt) {
textFieldSix.setText(" IOE ");
}
}
});
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geoff Jefferson wrote:I caught it here. I suppose I had better learn to output the stack trace.



Just stick in

evt.printStackTrace();

and then come back and show us what you get.
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nifty. Access denied.
I think windows has denied file access.
I should be able to fix that.


 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well...I fixed the access problem. Now this.




 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so sun.swing.table.DefaultTableCellHeaderRenderer$EmptyIcon is not serializable -- i.e., you can't write one to an ObjectOutputStream. So the next question is, what are you trying to accomplish here? You may have to do it another way.
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write the contents of an ArrayList to a file on the hard drive.
Here is the file that contains the List.

The class ItemFile is after that.





 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's this "MyTableInterface"? Is it serializable as well?
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what do you want to do with the file when you read it back in? I suspect you don't really want to save the MyTableInterface object, right? Maybe you can mark that member variable "transient", so it's not saved, and then re-attach it to the MyTableInterface when the file is loaded back in?
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So what do you want to do with the file when you read it back in?



When I read it back in, I want to put it back into the ArrayList.


I suspect you don't really want to save the MyTableInterface object, right?


hhhhmmmmm.


Maybe you can mark that member variable "transient", so it's not saved, and then re-attach it to the MyTableInterface when the file is loaded back in?


I need to study up on transient. I want an ItemFile object to go to the ArrayList for use while the program is using it. Then to store it on disc when not.
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though MyTableInterface extends JPanel and therefore implements Serializable, it is definitely not serializable. It has some input and output streams as fields, and those are definitely not serializable - and therefore neither is MyTableInterface. Consider transient like Ernest suggested.
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Even though MyTableInterface extends JPanel and therefore implements Serializable, it is definitely not serializable. It has some input and output streams as fields, and those are definitely not serializable - and therefore neither is MyTableInterface. Consider transient like Ernest suggested.



Alright, I read up on object serialization and if I am correct, I need to implement the java.io.serializable interface in the persistent class. In this case it would be ItemFile, so...

[code]


package mytablemain;

import java.io.Serializable;
import javax.swing.JTextField;

class ItemFile implements Serializable{
ItemFile() {}
MyTableInterface ifObj = new MyTableInterface();

String one;
String two;
String three;
String four;
String five;
String six;

public ItemFile(JTextField textOne, JTextField textTwo, JTextField textThree,
JTextField textFour, JTextField textFive, JTextField textSix)
{
one = textOne.getText();
two = textTwo.getText();
three = textThree.getText();
four = textFour.getText();
five = textFive.getText();
six = textSix.getText();
}

}

[\code]

Then I need to persist the object by calling the ObjectOutputStream.writeObject() method. Which I have done in the following file.

[code]

buttonThree.addActionListener( // write listArray to file
new ActionListener() {
public void actionPerformed( ActionEvent e) {

try {
fOut = new FileOutputStream ("C:\\Jeff\\NetBeans Gui Building\\MyTableMain\\DataFiles");
oOS = new ObjectOutputStream(fOut);

for (ItemFile obj: tableModel.getList()) { // for-each loop
oOS.writeObject(obj);
}
}
catch(IOException evt) {
textFieldSix.setText(" IOE ");
evt.printStackTrace();
}
}
});
[\code]

I still get the following error output:

java.io.NotSerializableException: sun.swing.table.DefaultTableCellHeaderRenderer$EmptyIcon
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)

... and so on.
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please disregard, I found an error in ItemFile.

Geoff
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic