Here's my code:
try {
Uri uri = Uri.parse("file:///storage/emulated/0/Download/information.csv");
File file = new File(uri.getPath());
//Read the file
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
boolean firstLine = true;
while ((line = br.readLine()) != null) {
System.out.println("Print the contents from the file :" + line);
if (firstLine) {
firstLine = false;
continue;
} else {
//deserialize the string to Object
td = TransferData.fromString(line); //Where td is an object
//deserialize the string to Object
System.out.println("deserialized: " + td);
}
However, I get an exception on this line:
td = TransferData.fromString(line);
and from my fromString function:
/** Read the object from Base64 string. */
public static TransferData fromString( String s ) throws IOException, ClassNotFoundException {
byte [] data = Base64.decode(s, Base64.DEFAULT);
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readObject();
ois.close();
return (TransferData)o;
}
The exception is StreamCorruptedException but I'm not sure why that is. I want to be able to read in a string and deserialize the string.
EDIT:
/** Write the object to a Base64 string. */
public static String toString(Serializable o) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(output);
oos.writeObject(o);
oos.close();
return new String(Base64.encode(output.toByteArray(), Base64.DEFAULT));
}
//SerialVersionID
private static final int serialVersionUID = 10032014;