This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes ? File I/O (and-or) for each loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "? File I/O (and-or) for each loop" Watch "? File I/O (and-or) for each loop" New topic
Author

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

Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Does this make sense?



Get list being in the following file.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It writes each object in the list to an ObjectOutputStream. Makes perfect sense, why?


[Jess in Action][AskingGoodQuestions]
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
I'm catching an I/O Exception.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Apr 09, 2009
Posts: 102
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Apr 09, 2009
Posts: 102

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


Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102

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




Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Apr 09, 2009
Posts: 102
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.





Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

What's this "MyTableInterface"? Is it serializable as well?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Apr 09, 2009
Posts: 102

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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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

Joined: Apr 09, 2009
Posts: 102

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

Joined: Apr 09, 2009
Posts: 102
Please disregard, I found an error in ItemFile.

Geoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ? File I/O (and-or) for each loop
 
Similar Threads
Concerning Custom Table Model
? JTable getColumnName
I don't understand why my JTable will not display data
Help with AbstractTableModel - Please!!
Refreshing a window