I am trying to serialize a TreeMap to a text file but I cannot get it to work. I have tried printing/debugging and reading Javadocs.
My Record and Table class both implement serializable and I've also built a custom comparator which also implements serializable for my new TreeMap<>().
I believe I have narrowed down the issues to:
- I am trying to serialize Map.Entry, which might not be serializable and hence the NotSerializableExceptions.
- TreeMap is/ is not serializable (?).
- (Unknown Source) - unsure what Unknown Source is doing to my writeObject java.io.ObjectInputStream.readObject0(Unknown Source) and java.io.ObjectInputStream.writeObject0(Unknown Source)
My primary goal is to eliminate the NotSerializableExceptions by correctly serializing my Map.Entry or my TreeMap when i writeObject().
A secondary question would be what the 'readObject0(Unknown Source) ' is. In other cases resembling my issue, there was an ObjectOutputStream:1356 where my unknown source is.
I'm currently using Java 8.
class Record implements Serializable{
private String[] row;
class Table implements Serializable {
private TreeMap<String, Record> tm = new TreeMap<>(
new ReverseNaturalOrderComparator());
private String[] columns;
private int totalRecords;
[...]
[...]
[...]
void insertRecord( String key, Record r ) {
if( r.rowArrayLength() == numRowFields ) {
tm.put(key, r);
totalRecords++;
}
else {
System.out.println("Number of fields do not match. " +
"Please enter a record with " + numRowFields + " fields");
}
}
class Files {
File file = new File("KeyValues.txt");
void writeFile(Table t) throws IOException {
TreeMap<String, Record> ftm = t.getTreeMap();
FileOutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
Set set = ftm.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry<String, Record> keyValue = (Map.Entry<String, Record>)i.next();
if( !file.exists() ) { System.out.println("No such file exists"); }
else { oos.writeObject(keyValue); } // <---causing error
}
oos.close();
os.close();
}
The following code block within class Files is causing an error: Iterator i = set.iterator(); while(i.hasNext()) {
Map.Entry<String, Record> keyValue = (Map.Entry<String, Record>)i.next();
if( !file.exists() ) { System.out.println("No such file exists"); }
else { oos.writeObject(keyValue); } // <---causing error
}
oos.close();
os.close();
} main():
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Table t = new Table();
Record test0 = new Record("We are the champions, my friend.");
Record test1 = new Record("hope this works, but might not");
Record test2 = new Record("sweet, yellow bananas");
Record test3 = new Record("and bad mistakes Ihave made afew");
Record test4 = new Record("Now using and printing a TreeMap<>");
Record test5 = new Record("Exploring Antarctica alongside the polar bears");
t.insertRecord("3",test3);
t.insertRecord("0",test0);
t.insertRecord("2",test2);
t.insertRecord("4",test4);
t.insertRecord("1",test1);
t.insertRecord("5",test5);
try { writeFile(t); } //this is giving me the error.
Error Log
java.io.NotSerializableException: java.util.TreeMap$Entry
Final filepath : ...KeyValues.txt
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Files.writeFile(Files.java:52)
at Files.test(Files.java:87)
at Files.run(Files.java:64)
at Files.main(Files.java:62)
java.io.WriteAbortedException: writing aborted;
java.io.NotSerializableException: java.util.TreeMap$Entry
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Table.readFile(Table.java:92)
at Files.test(Files.java:100)
at Files.run(Files.java:64)
at Files.main(Files.java:62)
Caused by: java.io.NotSerializableException: java.util.TreeMap$Entry
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Files.writeFile(Files.java:33)
at Files.test(Files.java:66)
at Files.run(Files.java:48)
at Files.main(Files.java:46)