Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to write Arraylist to text file

843785Jan 1 2009 — edited Jan 7 2009
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;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 4 2009
Added on Jan 1 2009
9 comments
4,092 views