PDA

View Full Version : COde explanation



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.

i luffs yeww
05-02-2011, 09:19 AM
I'm a little confused on what you're asking? Unless you're just asking why the second try block doesn't open a file again? If so, they may just open the file in a previous part (the one you posted? :p), so there's no reason to open it again (it may mess stuff up if they edited it or didn't close it or what have you).

masterBB
05-02-2011, 10:29 AM
It opens the same file...

apx900
05-02-2011, 11:06 AM
thanks for the quick reply :)

the try blocks are 2 different methods. the first block is for displaying the first record. the second block is to display the next record. what confuses me is
record = (videoRecord)input.readObject(); what does it do? does it look for the first available object? or the next one? or what?

i luffs yeww
05-02-2011, 11:10 AM
I'm pretty sure it's executing readObject() on input and then casting the result as a videoRecord type and then setting that result to the "record" variable.

masterBB
05-02-2011, 01:55 PM
// 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);
}


I don't know enough to say for sure how it works. If I look at the first try-catch statement.

It creates a new FileInputStream, the file var, with an unknown value, probably contains the URL
Input is a ObjectInputStream, containing the FileInputStream, so you can do stuff with the file.
With Input.readObject() you retrieve the file and than typecast it to VideoRecord.


The file has been chosen outside this piece of code.
In the second try-cath, the complete input stream is handled elsewhere...

HyperSecret
05-02-2011, 04:29 PM
I don't understand your questions anymore...

This isn't anything that is general (or at least doesn't seem), so for anyone to be of any help (more than i luffs yeww), they would probably need to know what it is that the program is doing/suppose to do and have more source.

It's like me doing...



id = (treeID)FindIdByObj();


If you weren't part of SRL/RS cheating you wouldn't know what a treeID is or anything. Someone would only be able to tell you that there is a casting happening and not much more. This is the same situation that you are asking right now.

Not much more can be explain than what i luffs yeww has already said...

masterBB
05-02-2011, 05:06 PM
Well, it are just common functions.

But I'm sure that apx doesn't fully understand the other code. Both pieces of code don't open any file. They just load it and store that in the var record.

.readObject just read the last object that has been loaded in the ObjectInputStream. It returns a Object, that, before it can be used, has to be converted to a String, integer or in this code a VideoRecord. See the java documentation (http://download.oracle.com/javase/1.3/docs/api/java/io/ObjectInputStream.html#readObject())

The pointing to which file or videorecord has to be loaded is handled else where in the code. All I can say is that its handled before these pieces of code. And in the first piece of code its handled by a var named file, directing to an url. And in the second piece of code by a var input, which is a complete ObjectInputStream.

n3ss3s
05-02-2011, 08:19 PM
Trust me, it's gonna make fucktons of sense if you find where record is declared.

Cool Aide
05-02-2011, 09:09 PM
It is just reading the same file. It would be redundant to open the same file twice.