how to save all the arrayList`s object?
hi...all....i am going to make a simple CD Application...I got some function on my program ..inert the cd, delete the cd, display the cd..Let `s i want to insert the cd `s information from the console.(i can`t reach this part) But, how i should save all these data back to a file..i am stuck on passing those object to a file and save it after doing the insertaion..
pls help..
// problem
import java.io.*;
//This Demo , i try to pass new MusicCD object with its new parameter to a new object.And this object is going to be written
// in a file called "cdDataFile.dat" .After this, i want to reopen that file that i just create.
// i am done and understand how it work in object to be sent to a file and readObject from file...
// However, i have no idea idea how to save my arrayList `s object to a file.In other word, it is not save one object by one.
// i need to be save my arrayList depenting on the user`s input<<-(infinitely object)
// *pls help .....i stuck on how to save all the arrayList `s object to a file.
// Thc!!
//Note
//infinintely object and save to a file
publicclass ObjectIODemo
{
//save to a file
publicstaticvoid main(String[] args)
{
try
{
//ArrayList<MusicCd> results = new ArrayList<MusicCd>();
ObjectOutputStream outputStream =
new ObjectOutputStream(new FileOutputStream("cdDataFile.dat"));
//how should i change as the following in order to save my arrayList `s object to a file...
//now i am passing a object per time only...it is not what i want to do...
//i want to pass all the object in my arrayList to the outputStream and save it as a file ..
MusicCd object1 =new MusicCd("Jessica ", 1995);
MusicCd anOtherObject1 =new MusicCd("eddie " , 2001);
outputStream.writeObject(object1);
outputStream.writeObject(anOtherObject1);
System.out.println("Data sent to a file. ");
}
catch(IOException e)
{
System.out.println("Problem with file output. ");
}
System.out.println("Now let `s reopen the file and display the data");
try
{
ObjectInputStream inputStream =
new ObjectInputStream(new FileInputStream("cdDataFile.txt"));
MusicCd readOne = (MusicCd)inputStream.readObject();
MusicCd readTwo = (MusicCd)inputStream.readObject();
System.out.println("The following were read from the file :");
System.out.println(readOne);
System.out.println(readTwo);
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find datafile. ");
}
catch(ClassNotFoundException e)
{
System.out.println("Problems with file input. ");
}
catch(IOException e)
{
System.out.println("Problems with file input. ");
}
System.out.println("End of Program");
}
}
//the following code for better what i am try to do
import java.io.Serializable;
publicclass MusicCdimplements Serializable
{
private String musicCdsTitle;
privateint yearOfRelease;
public MusicCd()
{
musicCdsTitle ="";
yearOfRelease = 1900;
}
public MusicCd(String newMusicCdsTitle,int newYearOfRelease)
{
musicCdsTitle = newMusicCdsTitle;
yearOfRelease = newYearOfRelease;
}
public String getTitle()
{
return musicCdsTitle;
}
publicint getYearOfRelease()
{
return yearOfRelease;
}
publicvoid setTitle(String newMusicCdsTitle)
{
musicCdsTitle = newMusicCdsTitle;
}
publicvoid setYearOfRelease(int newYearOfRelease)
{
yearOfRelease = newYearOfRelease;
}
/*
public boolean equalsName(MusicCd otherCd)
{
if(otherCd == null)
return false;
else
return (musicCdsTitle.equals(otherCd.musicCdsTitle));
}
*/
public String toString()
{
return("Music Cd`s Title: " + musicCdsTitle +"\t"
+"Year of release: " + yearOfRelease +"\t");
}
}
import java.util.ArrayList;
import java.io.*;
publicclass MusicCdStore
{
ArrayList<MusicCd> MusicCdList;
publicvoid insertCd()
{
MusicCdList =new ArrayList<MusicCd>( );
readOperation theRo =new readOperation();
MusicCd theCd;
int muiseCdsYearOfRelease;
String muiseCdsTitle;
while(true)
{
String continueInsertCd ="Y";
do
{
muiseCdsTitle = theRo.readString("Please enter your CD`s title : ");
muiseCdsYearOfRelease = theRo.readInt("Please enter your CD`s year of release : ");
MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));
MusicCdList.trimToSize();
continueInsertCd = theRo.readString("Do you have another Cd ? (Y/N) : ");
}while(continueInsertCd.equals("Y") || continueInsertCd.equals("y") );
if(continueInsertCd.equals("N") || continueInsertCd.equals("n"));
{
//MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));
break;
}
//System.out.println("You `ve an invalid input " + continueInsertCd + " Please enter (Y/N) only!!");
}
}
publicvoid displayAllCd()
{
System.out.println("\nOur CD collection is: \n" );
System.out.println(toString());
}
public String toString( )
{
String result=" ";
for( MusicCd tempCd : MusicCdList)
{
result += tempCd.toString() +"\n";
}
return result;
}
publicvoid searchingMusicCd()
{
readOperation theRo =new readOperation();
String keyword = theRo.readString("Enter a CD `s Title you are going to search : ") ;
ArrayList<MusicCd> results = searchForTitle(keyword );
System.out.println("The search results for " + keyword +" are:" );
for(MusicCd tempCd : results)
System.out.println( tempCd.toString() );
}
//encapsulate the A
publicvoid deleteCd()
{
while(true)
{
String continueInsertCd ="Y";
do
{
readOperation theRo =new readOperation();
String keyword = theRo.readString("Please enter CD `s title to be delected from SMOA : ") ;
ArrayList<MusicCd> deleteMusicCdResult =searchForDeleteCdsTitle(keyword);
System.out.println("The CD that you just removed is " + keyword );
for(MusicCd tempCd : deleteMusicCdResult)
//System.out.println( tempCd.toString() );
MusicCdList.remove(tempCd);// <-- changed
//muiseCdsTitle = theRo.readString("Please enter your CD`s title : ");
//muiseCdsYearOfRelease = theRo.readInt("Please enter your CD`s year of release : ");
//MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));
MusicCdList.trimToSize();
continueInsertCd = theRo.readString("Do you have another Cds to be removed from SMOA ? (Y/N) : ");
}while(continueInsertCd.equals("Y") || continueInsertCd.equals("y") );
if(continueInsertCd.equals("N") || continueInsertCd.equals("n"));
{
//MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));
break;
}
//System.out.println("You `ve an invalid input " + continueInsertCd + " Please enter (Y/N) only!!");
}
}
private ArrayList<MusicCd> searchForTitle(String searchString)
{
ArrayList<MusicCd> searchResult =new ArrayList<MusicCd>();
for(MusicCd currentMusicCd : MusicCdList)
{
if((currentMusicCd.getTitle()).indexOf(searchString) != -1)
searchResult.add(currentMusicCd);
}
searchResult.trimToSize();
return searchResult;
}
private ArrayList<MusicCd> searchForDeleteCdsTitle(String searchString)
{
MusicCd tempCd =new MusicCd();
tempCd.setTitle(searchString);
ArrayList<MusicCd> deleteCdsResult =new ArrayList<MusicCd>();
for(MusicCd currentMusicCd : MusicCdList)
{
if((currentMusicCd.getTitle()).equals(tempCd.getTitle()))
deleteCdsResult.add(currentMusicCd);
}
deleteCdsResult.trimToSize();
return deleteCdsResult;
}
}
/*
private ArrayList<MusicCd> searchForRemoveCdsTitle(String searchString)
{
ArrayList<MusicCd> removeCdsResult = new ArrayList<MusicCd>();
for(MusicCd currentMusicCd : MusicCdList)
{
if((currentMusicCd.getTitle()).indexOf(searchString) != -1)
removeCdsResult.remove(currentMusicCd);
}
removeCdsResult.trimToSize();
return removeCdsResult;
}
*/
import java.util.*;
publicclass MusicCdStoreEngine{
publicstaticvoid main(String[] args)
{
MusicCdStore mcs =new MusicCdStore( );
mcs.insertCd();
//save the data
//display the Cd that you just insert
mcs.displayAllCd();
mcs.deleteCd();
mcs.displayAllCd();
mcs.searchingMusicCd();
}
}
import java.util.*;
publicclass readOperation{
public String readString(String userInstruction)
{
String aString =null;
try
{
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
aString = scan.nextLine();
}
catch (NoSuchElementException e)
{
//if no line was found
System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
}
catch (IllegalStateException e)
{
// if this scanner is closed
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aString;
}
publicchar readTheFirstChar(String userInstruction)
{
char aChar =' ';
String strSelection =null;
try
{
//char charSelection;
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
strSelection = scan.next();
aChar = strSelection.charAt(0);
}
catch (NoSuchElementException e)
{
//if no line was found
System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
}
catch (IllegalStateException e)
{
// if this scanner is closed
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aChar;
}
publicint readInt(String userInstruction){
int aInt = 0;
try{
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
aInt = scan.nextInt();
}catch (InputMismatchException e){
System.out.println("\nInputMismatchException error occurred (the next token does not match the Integer regular expression, or is out of range) " + e);
}catch (NoSuchElementException e){
System.out.println("\nNoSuchElementException error occurred (input is exhausted)" + e);
}catch (IllegalStateException e){
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aInt;
}
}

