Я пытаюсь Serialize
и Deserialize
Hashtable
, но безуспешно.
Вот код:
Десериализация
public static void read(File f) throws IOException, ClassNotFoundException
{
FileInputStream fos = new FileInputStream(f);
ObjectInputStream oos = new ObjectInputStream(fos);
list = new Hashtable<Date,String>((Hashtable<Date,String>)oos.readObject());
oos.close();
}
Сериализация
public static void write(String s) throws FileNotFoundException, IOException
{
FileOutputStream fos = new FileOutputStream(s);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
}
Я написалClass
Дата, это не Java, но я сделал implemet Serializable
там.
После того, как я Deserialize
Hashtable
и распечатал его, я получаю только {}
.
Что я делаю не так?
РЕДАКТИРОВАТЬ
Основной класс:
public class Main implements Serializable
{
public static void main(String[] args) throws ClassNotFoundException, IOException
{
String[] options = {"Existing file","New file"};
int choice = JOptionPane.showOptionDialog(null,
"Whould you like to use an existing file or make a new file?",
"Reminders", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
System.out.println(choice);
if(choice == 0) //existing file
{
JFileChooser fc = new JFileChooser();
try
{
Thread.sleep(1);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
int returnVal = fc.showOpenDialog(null);
GUI.read(fc.getSelectedFile());
}
else if(choice == 1) //new file
{
String name;
do { name = JOptionPane.showInputDialog("Please enter the name of the new file: "); }
while((name == null) || (name.length() == 0));
GUI.write(name);
}
else if(choice == -1)
{
JOptionPane.showMessageDialog(null, "Good Bye!");
System.exit(0);
}
Класс GUI:
public class GUI extends JPanel implements Serializable
{
private DateGUI date;
private JButton save;
private static JTextArea text;
private JScrollPane scroll;
private static Hashtable<Date,String> list = new Hashtable<Date,String>;
public static void read(File f) throws IOException, ClassNotFoundException
{
FileInputStream fos = new FileInputStream(f);
ObjectInputStream oos = new ObjectInputStream(fos);
list = ((Hashtable<Date,String>)oos.readObject());
oos.close();
}
public static void write(String s) throws FileNotFoundException, IOException
{
FileOutputStream fos = new FileOutputStream(s);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
}
private class SaveListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == save)
{
list.put(new Date(DateGUI.getDay(),DateGUI.getMonth(),DateGUI.getYear()), text.getText());
text.setText("");
}
}
}
}
Дата Класс: имеет 3 String
полей дня, месяца, года.Я сделал имплементацию Serializable и переопределил equals
и HashCode
.
DateGUI Класс: имеет GUI
"вещи" и:
public static String getDay()
{
return (String)day.getSelectedItem();
}
public static String getMonth()
{
return (String)month.getSelectedItem();
}
public static String getYear()
{
return (String)year.getSelectedItem();
}
private class ShowListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == show)
{
GUI.set(GUI.get(getDay(), getMonth(), getYear()));
}
}
}