How do i go about writing the contents stored in a arraylist to a text file,
i got some rough code here..i can get stuff in the arraylist
but am stuck figuring how to get all that stuff into a text file
does the objectOutputStream work on the arraylist or just normal arrays..
do i need to use the serializable interface
any help will be much appreciated..
cheers
public void writeFile() throws IOException{
String fileString = "CDDatabase.txt";
File file01 = new File(fileString);
try {
fileInputStream = new FileInputStream(file01);
objectInputStream = new ObjectInputStream(fileInputStream) ;
objectOutputStream.writeObject(items.get(0)); //Arraylist
objectOutputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addCD(){
try
{
title = titleTextField.getText();
time = Integer.valueOf(timeTextField.getText());
price = Double.valueOf(priceTextField.getText());
comment = commentTextField.getText();
artist = artistTextField.getText();
track = Integer.valueOf(tracksTextField.getText());
cd02 = new CD(title,time,price,comment,artist,track);
items.add(cd02); //Arraylist
JOptionPane.showMessageDialog(null,items.size());
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(f,"Incorrect Input","Error Message",JOptionPane.ERROR_MESSAGE);
return;
}
}