Results 1 to 3 of 3
- 05-20-2012, 05:43 PM #1
Member
- Join Date
- May 2012
- Posts
- 1
- Rep Power
- 0
writing and Reading a serialized java object
/*hey guys i'm new to java and i have been given an exercise to make a cd collection, write it into a file and read the data back to the program.
002
the program is suppose to show you a menu to select from where you can add, delete, view sort, CD's when you add a CD it must be written to a file as an Object and when you want to view CDs or search for a CD the program must read the CD objects from the file they have been written to and must return a cd nam, artist and release date. the code looks like it is writing the Cd to a file but when i try to read (view or search for a cd from the file it gives an error null). so i think i'm note reading the right way.
003
thank you for trying to help.
004
005
*/
006
007
import java.util.*;
008
009
public class hipHopCollection {
010
011
ArrayList<cd> list = new ArrayList <cd> ();
012
013
EasyIn ei = new EasyIn();
014
015
private cd invoke;
016
private int b;
017
018
public void load()
019
{
020
System.out.println(" You Entered " + b + " To Add A CD ");
021
invoke = new cd();
022
System.out.println("Please Enter A CD Name ");
023
invoke.setName(ei.readString());
024
System.out.println("Please Enter A CD Price");
025
invoke.setPrice(ei.readDouble());
026
System.out.println("Please Give Ratings For The CD");
027
invoke.setRatings(ei.readInt());
028
System.out.println("Please Enter A CD release date ");
029
invoke.setReleaseDate(ei.readString());
030
System.out.println("Please Enter artist Name ");
031
invoke.setArtist(ei.readString());
032
System.out.println("Please Enter A CD Genre ");
033
invoke.setGenre(ei.readString());
034
list.add(invoke); // trying to add cd information to invoke.
035
036
037
}// end of load
038
039
// The following method should return the Object variable invoke that holds the cd INFO
040
041
public Object getInvoke()
042
{
043
return invoke;
044
}
045
//.................................................. ......................................
046
047
public int getB()
048
{
049
return b;
050
}
051
052
public void setB()
053
{
054
b=ei.readInt();
055
}
056
057
public void menu(){
058
System.out.println(".............................. ........................... ");
059
System.out.println("Hi There Please Enter A Number For Your Choice");
060
System.out.println(" Pess >>");
061
System.out.println("1 >> Add A CD");
062
System.out.println("2 >> View List Of CD's");
063
System.out.println("3 >> Sort CD's By Price");
064
System.out.println("4 >> Search CD By Name");
065
System.out.println("5 >> Remove CD(s) By Name");
066
System.out.println("0 >> Exit");
067
System.out.println(".............................. ...........................");
068
System.out.print("Please Enter Chioce >> ");
069
070
}// end of menu
071
072
public void GoodBye()
073
{
074
System.out.println(" You Entered " + b + " To exit Good_bye" );
075
System.exit(0);
076
}//end of GoodBye
077
078
public void PriceSort()
079
{
080
System.out.println(" You Entered " + b + " To Sort CD(s) By price ");
081
Collections.sort(list, new SortByPrice());
082
for(cd s : list)
083
System.out.println(s.getName() + ": " + s.getPrice());
084
}// end of PriceSort
085
086
087
public void NameSearch()
088
{
089
System.out.println(" You Entered " + b + " To Search CD(s) By Name ");
090
System.out.println("Please Enter The Name Of The CD You Are Searching For " );
091
String search = ei.readString();
092
for(int i=0; i<list.size();i++){
093
if(search.equalsIgnoreCase(list.get(i).getName() )){
094
System.out.println(list.get(i).getName() + " " + list.get(i).getPrice() + " " + list.get(i).getRatings() + " " + list.get(i).getGenre() );
095
096
}
097
}
098
}//end of NameSearch
099
100
public void ViewList()
101
{
102
System.out.println(" You Entered " + b + " To view CD(s) By Name ");
103
for(int i=0; i<list.size();i++)
104
{
105
System.out.println(list.get(i).getName() + " " + list.get(i).getPrice() + " " + list.get(i).getRatings() + " " + list.get(i).getGenre() );
106
}
107
}// end of ViewList
108
109
public void DeleteCd()
110
{
111
System.out.println(" You Entered " + b + " To Delete CD(s) By Name ");
112
System.out.println("Please Enter The Name Of The CD You Want to Delete ");
113
String search = ei.readString();
114
for(int i=0; i<list.size();i++)
115
{
116
if(search.equalsIgnoreCase(list.get(i).getName() ))
117
{
118
System.out.println(list.get(i).getName());
119
list.remove(i);
120
}
121
}
122
}// end of DeleteCD
123
//.................................................. ..................................
124
public static void main(String[] args) {
125
//creating an Instance of EasyIn by object ei. Easy in is a Scanner class for reading
126
EasyIn ei = new EasyIn();
127
128
129
ArrayList<cd> list = new ArrayList <cd> (); // creating an array cd list
130
131
hipHopCollection call = new hipHopCollection();
132
133
ReadWrite rw = new ReadWrite();
134
while (true){
135
call.menu();
136
call.setB();
137
//b = ei.readInt();
138
if(call.getB()==0)
139
{
140
call.GoodBye();
141
}
142
if(call.getB()==1)
143
{
144
call.load();
145
rw.doWriting();// trying to write the cd object to a file
146
}
147
if(call.getB()==2)
148
{
149
rw.doReading();// trying to read the cd object from a file
150
//call.ViewList();
151
}
152
if(call.getB()==3)
153
{
154
call.PriceSort();
155
}
156
if(call.getB()==4)
157
{
158
call.NameSearch();
159
}
160
if(call.getB()==5)
161
{
162
call.DeleteCd();
163
}
164
165
166
}// end of while
167
}// end of main
168
}// end of class
169
170
import java.io.Serializable;
171
public class cd implements Serializable {
172
173
//creating attributes
174
private String cdname = null;
175
private double price = 0.0;
176
private String artist =null;
177
private int ratings =0;
178
private String genre=null;
179
private String releaseDate =null;
180
// creating an Empty constructor
181
182
public cd(){
183
184
}
185
186
public cd (String cdname,double price, int ratings, String genre, String artist, String releaseDate){
187
this.cdname=cdname;
188
this.price=price;
189
this.artist=artist;
190
this.ratings=ratings;
191
this.genre=genre;
192
this.releaseDate=releaseDate;
193
}
194
195
public String getGenre(){
196
return genre;
197
}
198
public void setGenre(String genre){
199
200
this.genre =genre;
201
}
202
public String getArtist(){
203
204
return artist;
205
}
206
207
public void setArtist(String artist){
208
this.artist=artist;
209
}
210
public String getName(){
211
return cdname;
212
}
213
public void setName(String cdname){
214
this.cdname = cdname;
215
}
216
public Double getPrice(){
217
return price;
218
}
219
public void setPrice(double price){
220
this.price = price;
221
}
222
public String getReleaseDate(){
223
return releaseDate;
224
}
225
public void setReleaseDate(String releaseDate){
226
this.releaseDate = releaseDate;
227
}
228
public int getRatings(){
229
return ratings;
230
}
231
public void setRatings( int ratings){
232
this.ratings = ratings;
233
234
}
235
236
}
237
238
// importing all the packages that we will use
239
import java.io.ObjectInputStream;
240
import java.io.FileInputStream;
241
import java.io.File;
242
import java.io.FileNotFoundException;
243
import java.io.FileOutputStream;
244
import java.io.IOException;
245
import java.io.ObjectOutputStream;
246
import java.io.OutputStream;
247
import java.io.Serializable;
248
249
public class ReadWrite {
250
// these are all the attributes
251
private String FileName ="CdCollections.dat";
252
private OutputStream output;
253
private ObjectOutputStream oos;
254
private FileOutputStream fos;
255
private File file;
256
private FileInputStream fis;
257
private ObjectInputStream ois;
258
259
//creating an empty constructor
260
public ReadWrite()
261
{
262
// we could initialise all the attributes inside this empty constructor
263
}
264
265
//creating a constructor with arguments of a file name.
266
public ReadWrite(File file)
267
{
268
269
this.file=file;
270
271
try
272
{
273
//Use a FileOutputStream to send data to a file called CdCollections.dat
274
fos = new FileOutputStream(file,true);
275
/*
276
Use an ObjectOutputStream to send object data to the
277
FileOutputStream for writing to disk.
278
*/
279
oos = new ObjectOutputStream (fos);
280
fis=new FileInputStream(file);
281
ois = new ObjectInputStream(fis);
282
283
}
284
catch(FileNotFoundException e)
285
{
286
System.out.println("File Not Found");
287
}
288
catch(IOException a)
289
{
290
System.out.println(a.getMessage());
291
System.out.println("Please check file permissions of if file is not corrupt");
292
}
293
294
}// end of the second constructor
295
296
//the following lines of code will be the accessors and mutators
297
//.................................................. .................................................. ....
298
299
/**
300
* @return the output
301
*/
302
public OutputStream getOutput() {
303
return output;
304
}
305
306
/**
307
* @param output the output to set
308
*/
309
public void setOutput(OutputStream output) {
310
this.output = output;
311
}
312
313
/**
314
* @return the objStream
315
*/
316
public ObjectOutputStream getOos() {
317
return oos;
318
}
319
320
/**
321
* @param objStream the objStream to set
322
*/
323
public void setObjStream(ObjectOutputStream objStream) {
324
this.oos = oos;
325
}
326
public File getFile() {
327
return file;
328
}
329
330
public void setFile(File file) {
331
this.file = file;
332
}
333
334
public FileInputStream getFis() {
335
return fis;
336
}
337
338
public void setFis(FileInputStream fis) {
339
this.fis = fis;
340
}
341
342
public ObjectInputStream getOis() {
343
return ois;
344
}
345
346
public void setOis(ObjectInputStream ois) {
347
this.ois = ois;
348
}
349
// the following lines of code will be the methods for reading and writing
350
/*................................................. .......................................
351
352
the following method doWriting will write data from the hipHopCollections source code.
353
that will be all the cd information.
354
355
Pass our object to the ObjectOutputStream's
356
writeObject() method to cause it to be written out
357
to disk.
358
obj_out.writeObject (myObject);
359
360
.................................................. ......................................*/
361
public void doWriting()
362
{
363
hipHopCollection call = new hipHopCollection();
364
365
//creating an Object variable hold that will hold cd data from hipHopCollections invoke
366
367
Object hold = call.getInvoke();// THI COULD BE THE PART WHERE I MADE A MISTAKE
368
369
ReadWrite stream = new ReadWrite (new File(FileName));
370
371
try
372
{
373
374
/*
375
Pass our object to the ObjectOutputStream's
376
writeObject() method to cause it to be written out to disk.
377
stream.getOos().writeObject(hold);
378
*/
379
stream.getOos().writeObject(hold);
380
stream.getOos().close();
381
System.out.println("Done writing Object");
382
}
383
384
catch (IOException e)
385
{
386
System.out.println(e.getMessage());
387
System.out.println("Program Failed To Write To The File");
388
}
389
finally
390
{
391
System.out.println("The program Has come To An End GoodBye");
392
}
393
394
}// end of method DoWriting
395
396
/*................................................. .......................................
397
398
The following method is for reading data from the file written by the above method named
399
DoWriting
400
.................................................. .......................................*/
401
// PLEASE NOT THIS IS THE METHOD THAT GIVES ME NULL EXCEPTION
402
public void doReading()
403
{
404
ReadWrite read = new ReadWrite(new File(FileName));
405
try{
406
//System.out.println("I AM NOW INSIDE THE TRY TO READ");
407
Object obj = read.getOis().readObject();
408
System.out.println("tried reading the object");
409
cd c = (cd)obj; // trying to cast the object back to cd type
410
411
System.out.println("I have typed cast the Object");
412
System.out.println(c.getName());
413
System.out.println(c.getGenre());
414
System.out.println(c.getArtist());
415
System.out.println(c.getPrice());
416
System.out.println(c.getRatings());
417
System.out.println(c.getReleaseDate());
418
read.getOis().close();
419
420
421
}
422
catch(ClassNotFoundException e)
423
{
424
System.out.println(e.getMessage());
425
System.out.println("THE CLASS COULD NOT BE FOUND");
426
}
427
catch(IOException e)
428
{
429
System.out.println(e.getMessage());// null
430
System.out.println("WE COULD NOT READ THE DATA INSIDE THE FILE");
431
}
432
}//end of method doReading
433
}// end of class ReadWrite
- 05-20-2012, 06:48 PM #2
Re: writing and Reading a serialized java object
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-20-2012, 06:51 PM #3
Re: writing and Reading a serialized java object
Cross posted
https://forums.oracle.com/forums/thr...readID=2391393
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Serialized Java Obj testing
By akokane in forum New To JavaReplies: 0Last Post: 02-02-2011, 06:38 AM -
Size of serialized object with null values
By jamesd128 in forum New To JavaReplies: 1Last Post: 10-22-2010, 06:43 PM -
File reading / writing
By MattBSibley in forum New To JavaReplies: 5Last Post: 04-19-2010, 05:20 AM -
Reading and Writing to XML - Help Please!
By JonnySnip3r in forum New To JavaReplies: 4Last Post: 01-17-2010, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks