apx900
05-02-2011, 09:06 AM
hello!! i have recently started to learn java in my college. I was following a tutorial earlier and i ran into a bit of code that works but i dont quite umderstand.
// the first bit of code opens the file and read the object in the file.
try{
input = new ObjectInputStream(new FileInputStream(file)); // so this opens th file
record = (videoRecord)input.readObject(); // this then reads the first object
txtClassification.setText(record.getClassification ()); the rest of the code then get the info from the object and and displays it to the gui.
txtTitle.setText(record.getTitle());
txtDuration.setText(Integer.toString(record.getDur ation()));
txtHireCost.setText(Float.toString(record.getHireC ost()));
} catch (IOException e){
JOptionPane.showMessageDialog(this, "Failesd to find the file class", "IOException", JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(this, "Record calss not found", "ClassNotFoundException", JOptionPane.ERROR_MESSAGE);
}
////////////////////////next bit of code////////////////////////
preet much identical to the previous bit of code execpt dosent open the file
try{
record = (videoRecord)input.readObject();
txtClassification.setText(record.getClassification ());
txtTitle.setText(record.getTitle());
txtDuration.setText(Integer.toString(record.getDur ation()));
txtHireCost.setText(Float.toString(record.getHireC ost()));
} catch (IOException e){
JOptionPane.showMessageDialog(this, "End of file", "IOException", JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(this, "Record calss not found", "ClassNotFoundException", JOptionPane.ERROR_MESSAGE);
}
my problem is the bit of the code thats bold. how come in one bit it reads the first thing in the file and in the other bit it reads the next one?
if you would like a copy of the project, i am using netbeans. feel free to pm me and i'll upload it.
// the first bit of code opens the file and read the object in the file.
try{
input = new ObjectInputStream(new FileInputStream(file)); // so this opens th file
record = (videoRecord)input.readObject(); // this then reads the first object
txtClassification.setText(record.getClassification ()); the rest of the code then get the info from the object and and displays it to the gui.
txtTitle.setText(record.getTitle());
txtDuration.setText(Integer.toString(record.getDur ation()));
txtHireCost.setText(Float.toString(record.getHireC ost()));
} catch (IOException e){
JOptionPane.showMessageDialog(this, "Failesd to find the file class", "IOException", JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(this, "Record calss not found", "ClassNotFoundException", JOptionPane.ERROR_MESSAGE);
}
////////////////////////next bit of code////////////////////////
preet much identical to the previous bit of code execpt dosent open the file
try{
record = (videoRecord)input.readObject();
txtClassification.setText(record.getClassification ());
txtTitle.setText(record.getTitle());
txtDuration.setText(Integer.toString(record.getDur ation()));
txtHireCost.setText(Float.toString(record.getHireC ost()));
} catch (IOException e){
JOptionPane.showMessageDialog(this, "End of file", "IOException", JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(this, "Record calss not found", "ClassNotFoundException", JOptionPane.ERROR_MESSAGE);
}
my problem is the bit of the code thats bold. how come in one bit it reads the first thing in the file and in the other bit it reads the next one?
if you would like a copy of the project, i am using netbeans. feel free to pm me and i'll upload it.