我想通过序列化来保存和存储简单的邮件对象,但我总是遇到错误,我找不到它的位置.
package sotring; import java.io.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import com.sun.org.apache.bcel.internal.generic.INEG; public class storeing { public static void storeMail(Message[] mail){ try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("mail.ser")); out.writeObject(mail); out.flush(); out.close(); } catch (IOException e) { } } public static Message[] getStoredMails(){ try { ObjectInputStream in = new ObjectInputStream(new FileInputStream("mail.ser")); Message[] array = (Message[]) in.readObject() ; for (int i=0; i< array.length;i++) System.out.println("EMail von:"+ array[i].getSender() + " an " + array[i].getReceiver()+ " Emailbetreff: "+ array[i].getBetreff() + " Inhalt: " + array[i].getContent()); System.out.println("Size: "+array.length); //return array; in.close(); return array; } catch(IOException ex) { ex.printStackTrace(); return null; } catch(ClassNotFoundException ex) { ex.printStackTrace(); return null; } } public static void main(String[] args) { User user1 = new User("User1", "geheim"); User user2 = new User("User2", "geheim"); Message email1 = new Message(user1.getName(), user2.getName(), "Test", "Fooobaaaar"); Message email2 = new Message(user1.getName(), user2.getName(), "Test2", "Woohoo"); Message email3 = new Message(user1.getName(), user2.getName(), "Test3", "Okay =) "); Message [] mails = {email1, email2, email3}; storeMail(mails); Message[] restored = getStoredMails();; } }
这是用户和消息类
public class Message implements Serializable{ static final long serialVersionUID = -1L; private String receiver; //Empfänger private String sender; //Absender private String Betreff; private String content; private String timestamp; private String getDateTime() { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); return dateFormat.format(date); } Message (String receiver, String sender, String Betreff, String content) { this.Betreff= Betreff; this.receiver = receiver; this.sender = sender; this.content = content; this.timestamp = getDateTime(); } Message() { // Just for loaded msg } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getBetreff() { return Betreff; } public void setBetreff(String betreff) { Betreff = betreff; } public String getContent() { return content; } public String getTime() { return timestamp; } public void setContent(String content) { this.content = content; } } public class User implements Serializable{ static final long serialVersionUID = -1L; private String username; //unique Username private String ipadress; //changes everytime private String password; //Password private int unreadMsg; //Unread Messages private static int usercount; private boolean online; public String getName(){ return username; } public boolean Status() { return online; } public void setOnline() { this.online = true; } public void setOffline() { this.online = false; } User(String username,String password){ if (true){ this.username = username; this.password = password; usercount++; } else System.out.print("Username not availiable"); } public void changePassword(String newpassword){ password = newpassword; } public void setIP(String newip){ ipadress = newip; } public String getIP(){ if (ipadress.length() >= 7){ return ipadress; } else return "ip address not set."; } public int getUnreadMsg() { return unreadMsg; } }
这是一个例外:
exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type Message[]
at sotring.storeing.getStoredMails(storeing.java:22)
at sotring.storeing.main(storeing.java:57)
谢谢您的帮助!!!!!!!!!!!
catch子句需要返回一些东西.
public static Message[] getStoredMails(){ try { ObjectInputStream in = new ObjectInputStream(new FileInputStream("mail.ser")); Message[] array = (Message[]) in.readObject() ; System.out.println("Size: "+array.length); //return array; in.close(); return array; } catch(IOException ex) { ex.printStackTrace(); } catch(ClassNotFoundException ex) { ex.printStackTrace(); } return null; //fix }