我正在尝试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()));
}
}
}
答案 0 :(得分:2)
将
Hashtable
反序列化并打印后,我得到的只是{}
。我在做什么错了?
此代码是从您的MCVE代码复制而来的。此后,您已经对其进行了编辑,但是我坚信这是导致问题的真正原因。
public static void write(String s) throws FileNotFoundException, IOException
{
list = new Hashtable<Date,String>();
FileOutputStream fos = new FileOutputStream(s);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
}
您应该在close()
上致电oos
,但这不是真正的问题。
(缺少的close()
可能导致序列化文件为空或不完整/损坏,但这不会导致您读回空的Hashtable
。相反,请注意,new FileOutputStream(s)
将截断文件...)
真正的问题是方法的第一个陈述。您无条件将新的空白Hashtable
分配给list
。然后,您正在编写它。因此,(自然地)当您读回写到文件的内容时,您将再次得到一个空的Hashtable
。这就是您所看到的{}
的意思。
简而言之,您的代码正在写出一个空表并再次读取它。 Hashtable
序列化正在按预期进行。
答案 1 :(得分:1)
我相信您应该在写完后刷新。
oos.writeObject(list); oos.close();
答案 2 :(得分:0)
您是否还在向文件中写入任何内容,因为write方法将String作为参数,但是您尚未指定要写入哪个文件。试试:
public static void write(File f) throws FileNotFoundException, IOException
{
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Hashtable<Date, String> list = new Hashtable<>();
list.put(YourDateObject, "String");
oos.writeObject(list);
}
答案 3 :(得分:0)
这是代码示例:
public class Main {
/**
* File name
*/
private final static String FILENAME = "test.bin";
/**
* Entry point
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Hashtable<Date, String> original = new Hashtable<>();
// write some data to hashtable
for (int i = 0; i < 3; i ++) {
original.put(new Date(), String.valueOf(i));
TimeUnit.MILLISECONDS.sleep(100);
}
// serialize
write(FILENAME, original);
// deserialize
Hashtable<Date, String> restored = read(FILENAME);
// compare results
System.out.println(restored);
System.out.println(restored);
}
/**
* Deserialization
*
* @param filename
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Hashtable<Date, String> read(String filename) throws IOException, ClassNotFoundException {
try (ObjectInputStream oos = new ObjectInputStream(new FileInputStream(filename))) {
return (Hashtable<Date, String>) oos.readObject();
}
}
/**
* Serialization
*
* @param filename
* @param list
* @throws FileNotFoundException
* @throws IOException
*/
public static void write(String filename, Hashtable<Date, String> list) throws FileNotFoundException, IOException {
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeObject(list);
}
}
}