Java serialization will be less space efficient in certain cases than simply writing to a CSV file because it stores extra metadata to identify class-types.
I verified such a scenario with two simple test programs. The first one writes an array of ints to a .csv file.
import java.io.*;
public class CSVDemo {
public static void main(String [] args) {
try {
PrintWriter pw = new PrintWriter(new File("dummy.csv"));
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 1000; i++){
sb.append(1);
sb.append(",");
}
pw.write(sb.toString());
pw.close();
System.out.printf("Data is saved in dummy.csv");
} catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
The second one serializes an object containing an array of ints to a .ser file.
import java.io.*;
public class SerializeDemo
{
public static void main(String [] args)
{
DummyData dummy = new DummyData();
try {
FileOutputStream fileOut = new FileOutputStream("dummy.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(dummy);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in dummy.ser");
} catch(IOException i) {
i.printStackTrace();
}
}
public static class DummyData implements java.io.Serializable{
int[] data = new int[1000];
public DummyData(){
for(int i = 0; i < 1000; i++){
data[i] = 1;
}
}
}
}
The .ser file took 4079 bytes. The .csv file took 2000 bytes. Granted, this is a slight simplification of your use case (I'm equating an int to your Row type), but the general trend should be the same.
Trying with larger numbers yields the same result. Using 100000 ints results in ~400KB for .ser and 200KB for .csv
However, as the below comment pointed out, if choosing random values for ints, the .csv actually grows larger.