0

I'm trying to write a HashMap with the FileOutputStream. This is what my code looks like.

    public class ObjectStream implements Serializable{
        public void serialize(HashMap<String, Mat> obj){
            try {
                FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Juergen\\fileoutputstream.txt");
                ObjectOutputStream out = new ObjectOutputStream(fileOutput);
                out.write(obj);
                out.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

        }
}

The Problem is that the "write" function is not applicable for the argument. What can i do?Thanks

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66

3 Answers3

3

In addition to the previous answers, it has to be additionaly mention, that the Mat class originates from OpenCV. According to its Javadoc, it does not implement the Serializable interface. Thus, it will not be properly serializable via object serialization in Java.

Basically you can use a third-party object serialization library, which supports serialization without implementing Serializable. Related: Serializing a class, which does not implement Serializable

An other way to persist your data would be to implement your own custom file format in CSV or XML. For example:

key1
0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1

key2
0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1

This can be easily parsed / written using Apache Commons IO classes or JDK basic file io.

Community
  • 1
  • 1
rzo1
  • 5,561
  • 3
  • 25
  • 64
  • i would like to use JSON, do i need to format the object or can i just give it to json and let it do the serialization for me? – Jürgen K. Dec 15 '15 at 10:47
  • Take a look at: http://stackoverflow.com/questions/7539954/java-json-serialization-best-practice – rzo1 Dec 15 '15 at 11:16
1

Use the writeObject() method instead of the write() method:

out.writeObject(obj);
Maxim Sharai
  • 604
  • 3
  • 11
  • Yes i have. The problem is that when i load the data with the FileInputReader the same wait, it is not the same any more. So i get a floating point matrix exception that i don't get for the matrix before saving – Jürgen K. Dec 14 '15 at 14:24
  • Is your matrix implements Serializable? – Maxim Sharai Dec 14 '15 at 17:16
  • 1
    Implementing Serializable is mandatory to use object serialization. I suggest to write it out in a custom file-format (like csv or xml) – rzo1 Dec 15 '15 at 07:36
1

If you use ObjectOutputStream to serialize your data, it's important that you call the correct write*() method depending on the type of data you want to store. See the JavaDoc for ObjectOutputStream.writeObject()

        try {
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Juergen\\fileoutputstream.txt");
            ObjectOutputStream out = new ObjectOutputStream(fileOutput);
            out.writeObject(obj); //Writes an Object!
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
Jan
  • 13,738
  • 3
  • 30
  • 55