Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Timmie Nilsson
Timmie Nilsson
4,463 Points

Decipher or deserialize a String array.

Hi I am in the serializations Java Data project and in my workspace I only get back wierd coding when im reading them back from a file like this from consol: Tweet: "Hej, har ni hört senaste nyheten?" by Timmie Nilsson on Fri Mar 23 12:03:19 UTC 2018
Tweet: "This is so fuuuun!" by journeytocode on Fri Mar 23 12:03:19 UTC 2018
[Lcom.dfsweb.Tweet;@2d98a335
[Lcom.dfsweb.Tweet;@2d98a335

As you can see it works fine when i make a for loop without saving them and reading them back from a file.

Example.Java
import com.dfsweb.Tweet;
import com.dfsweb.Tweets;
import java.util.Date;
import java.util.Arrays;
public class Example {

  public static void main(String[] args) {
  Tweet tweet = new Tweet(
                    "Timmie Nilsson",
                    "Hej, har ni hört senaste nyheten?",
                    new Date()
  );

  Tweet secoundTweet = new Tweet(
                           "journeytocode",
                           "This is so fuuuun!",
                            new Date()
  );


  Tweet[] tweetArr = {tweet, secoundTweet};
    Arrays.sort(tweetArr);
    for (Tweet exampleTweet : tweetArr){
      System.out.println(exampleTweet);
    }

    Tweets.save(tweetArr);

    Tweet[] reloadedTweets = Tweets.load();
    for(Tweet tempTweets : reloadedTweets){
    System.out.println(reloadedTweets);
    }

  }
}
Tweet.java
package com.dfsweb;

import java.util.Date;
import java.io.Serializable;
public class Tweet implements Comparable, Serializable {

 private String mAuthor;
 private String mDescription; 
 private Date mCreationDate;

 public Tweet (String author, String description, Date creationDate){
   mAuthor = author;
   mDescription  = description;
   mCreationDate = creationDate;
  }


  @Override
  public String toString(){
    return String.format("Tweet: \"%s\" by %s on %s", 
                         mDescription, mAuthor, mCreationDate);
  }

  @Override
  public int compareTo(Object obj){
  Tweet other = (Tweet) obj;
    if(equals(other)){
      return 0;
    }
    int dateCmp = mCreationDate.compareTo(other.mCreationDate);
    if (dateCmp == 0){
      return mDescription.compareTo(other.mDescription);
    }
     return dateCmp;
  }

  public String getAuthor(){
    return mAuthor;
  }

  public String getDescription(){
    return mDescription;
  }

  public Date getCreationDate(){
    return mCreationDate;
  }

  public String[] getWords(){
  return mDescription.toLowerCase().split("[^\\w#@'åäö]+");
  }


}
Tweets.java
package com.dfsweb;

import java.io.*;

public class Tweets {
  public static void save(Tweet[] tweets){
    try (
      FileOutputStream fos = new FileOutputStream("tweets.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      ) {
      oos.writeObject(tweets);
      } catch(IOException ioe) {
        System.out.println("Problem saving");
        ioe.printStackTrace();
    } 
  }

  public static Tweet[]load() {
    Tweet[] tweets = new Tweet[0];

    try(
      FileInputStream fis = new FileInputStream("tweets.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
    ) {
      tweets = (Tweet[]) ois.readObject();
    } catch(IOException ioe) {
        System.out.println("Problem loading");
        ioe.printStackTrace();
    } catch(ClassNotFoundException cnfe) {
        System.out.println("Error loading tweets");
        cnfe.printStackTrace();
    }


    return tweets;
  }

}

1 Answer