code_junkie

Java: Serializing beginner problem :-(

Nov 11th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. package sotring;
  2. import java.io.*;
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6.  
  7. import com.sun.org.apache.bcel.internal.generic.INEG;
  8.  
  9. public class storeing {
  10.  
  11. public static void storeMail(Message[] mail){
  12. try {
  13. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("mail.ser"));
  14. out.writeObject(mail);
  15. out.flush();
  16. out.close();
  17.  
  18. } catch (IOException e) {
  19. }
  20. }
  21.  
  22. public static Message[] getStoredMails(){
  23.  
  24. try
  25. {
  26.  
  27. ObjectInputStream in = new ObjectInputStream(new FileInputStream("mail.ser"));
  28. Message[] array = (Message[]) in.readObject() ;
  29. for (int i=0; i< array.length;i++)
  30. System.out.println("EMail von:"+ array[i].getSender() + " an " + array[i].getReceiver()+ " Emailbetreff: "+ array[i].getBetreff() + " Inhalt: " + array[i].getContent());
  31.  
  32. System.out.println("Size: "+array.length); //return array;
  33. in.close();
  34. return array;
  35. }
  36. catch(IOException ex)
  37. {
  38. ex.printStackTrace();
  39. return null;
  40. }
  41. catch(ClassNotFoundException ex)
  42. {
  43. ex.printStackTrace();
  44. return null;
  45. }
  46. }
  47.  
  48. public static void main(String[] args) {
  49. User user1 = new User("User1", "geheim");
  50. User user2 = new User("User2", "geheim");
  51.  
  52. Message email1 = new Message(user1.getName(), user2.getName(), "Test", "Fooobaaaar");
  53. Message email2 = new Message(user1.getName(), user2.getName(), "Test2", "Woohoo");
  54. Message email3 = new Message(user1.getName(), user2.getName(), "Test3", "Okay =) ");
  55. Message [] mails = {email1, email2, email3};
  56. storeMail(mails);
  57. Message[] restored = getStoredMails();;
  58. }
  59. }
  60.  
  61. public class Message implements Serializable{
  62.  
  63. static final long serialVersionUID = -1L;
  64.  
  65. private String receiver; //Empfänger
  66. private String sender; //Absender
  67. private String Betreff;
  68. private String content;
  69. private String timestamp;
  70.  
  71. private String getDateTime() {
  72. DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  73. Date date = new Date();
  74. return dateFormat.format(date);
  75. }
  76.  
  77. Message (String receiver, String sender, String Betreff, String content) {
  78. this.Betreff= Betreff;
  79. this.receiver = receiver;
  80. this.sender = sender;
  81. this.content = content;
  82. this.timestamp = getDateTime();
  83. }
  84.  
  85. Message() { // Just for loaded msg
  86. }
  87.  
  88. public String getReceiver() {
  89. return receiver;
  90. }
  91.  
  92. public void setReceiver(String receiver) {
  93. this.receiver = receiver;
  94. }
  95.  
  96. public String getSender() {
  97. return sender;
  98. }
  99.  
  100. public void setSender(String sender) {
  101. this.sender = sender;
  102. }
  103.  
  104. public String getBetreff() {
  105. return Betreff;
  106. }
  107.  
  108. public void setBetreff(String betreff) {
  109. Betreff = betreff;
  110. }
  111.  
  112. public String getContent() {
  113. return content;
  114. }
  115. public String getTime() {
  116. return timestamp;
  117. }
  118. public void setContent(String content) {
  119. this.content = content;
  120. }
  121.  
  122. }
  123.  
  124. public class User implements Serializable{
  125.  
  126. static final long serialVersionUID = -1L;
  127. private String username; //unique Username
  128. private String ipadress; //changes everytime
  129. private String password; //Password
  130. private int unreadMsg; //Unread Messages
  131. private static int usercount;
  132. private boolean online;
  133.  
  134. public String getName(){
  135. return username;
  136. }
  137. public boolean Status() {
  138. return online;
  139. }
  140.  
  141. public void setOnline() {
  142. this.online = true;
  143. }
  144. public void setOffline() {
  145. this.online = false;
  146. }
  147.  
  148. User(String username,String password){
  149. if (true){
  150. this.username = username;
  151. this.password = password;
  152. usercount++;
  153.  
  154. } else System.out.print("Username not availiable");
  155. }
  156.  
  157. public void changePassword(String newpassword){
  158. password = newpassword;
  159. }
  160.  
  161. public void setIP(String newip){
  162. ipadress = newip;
  163. }
  164.  
  165. public String getIP(){
  166. if (ipadress.length() >= 7){
  167. return ipadress;
  168. } else return "ip address not set.";
  169. }
  170.  
  171.  
  172.  
  173. public int getUnreadMsg() {
  174. return unreadMsg;
  175. }
  176. }
  177.  
  178. public static Message[] getStoredMails(){
  179.  
  180. try
  181. {
  182.  
  183. ObjectInputStream in = new ObjectInputStream(new FileInputStream("mail.ser"));
  184. Message[] array = (Message[]) in.readObject() ;
  185. System.out.println("Size: "+array.length); //return array;
  186. in.close();
  187. return array;
  188. }
  189. catch(IOException ex)
  190. {
  191. ex.printStackTrace();
  192. }
  193. catch(ClassNotFoundException ex)
  194. {
  195. ex.printStackTrace();
  196. }
  197. return null; //fix
  198. }
  199.  
  200. public static Message[] getStoredMails(){
  201.  
  202. try
  203. {
  204.  
  205. ObjectInputStream in = new ObjectInputStream(new FileInputStream("mail.ser"));
  206. Message[] array = (Message[]) in.readObject() ;
  207. System.out.println("Size: "+array.length); //return array;
  208. in.close();
  209. return array;
  210. }
  211. catch(IOException ex)
  212. {
  213. ex.printStackTrace();
  214. }
  215. catch(ClassNotFoundException ex)
  216. {
  217. ex.printStackTrace();
  218. }
  219.  
  220. return null;
  221. }
Add Comment
Please, Sign In to add comment