I want to create two methods. The one should store and arraylist to the cache and the other should retrieve the arraylist from the cache. The code that I’ve tried is the following:
public class ByteConverter<T> {
public byte[] write_to_byte_array(ArrayList<T> list,File file){
// write to byte array
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
Log.d("file not found", "file not found", e1);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
for (T element : list) {
try {
out.writeUTF((String) element);
} catch (IOException e) {
Log.d("Error converting List to byte array", "Error converting List to byte array", e);
}
}
byte[] bytes = baos.toByteArray();
int len1 = 0;
while (len1 < bytes.length) {
try {
fos.write(bytes, 0, len1);
len1++;
} catch (IOException e) {
e.printStackTrace();
}
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}
public String read_to_byte_array(byte[] bytes) throws IOException{
// read from byte array
String element = null;
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream in = new DataInputStream(bais);
while (in.available() > 0) {
element = in.readUTF();
}
return element;
}
}
However the aforementioned methods doesn’t write anything to the file. I don’t know why. Can anyone help me? Also I’m taking a ClassCastException at this line:
out.writeUTF((String) element);
First, the
ClassCastExcetionis the reason you are not getting any output. The exception is thrown before your code writes to the file …Second, the
ClassCastExceptionis happening because the element is not aString. In theory, you could callelement.toString()to get a string representation, but the chances are that that representation will NOT be suitable for your purpose.So what is the correct approach?
Well it depends on the types of the objects in
list.If they implement
Serializable(orExternalizable) then you should be able to use Java serialization. Read this section of the Java tutorial for more details.Another option might be to use something like JAXP or GSON which can serialize POJOs to XML and JSON respectively.
The final fallback is to code the serialization method (and a corresponding serialization method) by hand.
Finally, I should point out some other errors in your code:
1) This code is wrong:
This will write:
The correct way to write
bytesis:or
2) Your exception handling logic is incorrect. In 3 or 4 places you catch an exception, print or log something … and then continue as if nothing had gone wrong. What you should probably do is let the exceptions propagate, and handle them at a higher level.
3) If something goes wrong in the
write_to_byte_arraymethod it is likely to leak a file descriptor. Best practice is to either close resources in afinallyblock, or use the new Java 7 try-with-resource syntax.4) The names of the
read_to_byte_arrayandwrite_to_byte_arraymethods violate the accepted Java style guidelines. They should bereadToByteArray(or better stillreadFromByteArray) andwriteToByteArray.