Ich habe ein Problem beim Schreiben einer ArrayList
an eine File
in Java. Das folgende Programm speichert einen Fehler in der Datei anstelle der Daten, die ich speichern möchte.Ausnahme beim Schreiben einer ArrayList in Datei
Unten ist der Code, den ich verwende. Kann jemand darauf hinweisen, was ich falsch mache?
package mytracker;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import javax.swing.JOptionPane;
public class ExportContacts
{
private ObjectOutputStream OP;
private void openFileOut(String path)
{
try
{
OP = new ObjectOutputStream(new FileOutputStream(path + ".dat"));
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
private void AddContacts(List<Contact> contacts)
{
try
{
for(int i=0;i<contacts.size();i++)
{
OP.writeObject(contacts.get(i));
}
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
private void CloseFileOut()
{
try
{
if(OP!=null)
OP.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
public void ExportConacts(String path,List<Contact> contacts)
{
openFileOut(path);
AddContacts(contacts);
CloseFileOut();
}
}
private void ExportButtonMouseClicked(java.awt.event.MouseEvent evt)
{
try
{
JFileChooser fileChooser=new JFileChooser();
//fileChooser.setFileFilter(new filter());
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result=fileChooser.showSaveDialog(this);
if(result!=JFileChooser.CANCEL_OPTION)
{
String path= fileChooser.getSelectedFile().getPath();
ExportContacts ex=new ExportContacts();
//CL is object of the class that save the contacts
List<Contact> c=CL.getContactsList();
ex.ExportConacts(path, c);
JOptionPane.showMessageDialog(null, "Contacts Exported Successfully !");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
Welche von 'Exception' wird geworfen? – twain249
Die Fehlermeldung in der Datei könnte nützlich sein. – ewanm89
Meine Kristallkugel sagt ** Kontakt ** implementiert nicht die ** java.io.Serializable ** Schnittstelle. –