Saving a string serialized and buffered android JAVA.IO ObjectInputStream - android

I am saving out and loading mixed data types. I either have the saving part wrong or the loading part wrong. I am using buffered serial save and load method.
Variable lastFetchDate is defined as a string and initialized as "00/00/00".
It throws an error when reloading the data after it has been saved. What is wrong? I would have thought the opposite to writeBytes would be readBytes for a string.
Saving is as follows:
FileOutputStream fos = new FileOutputStream("userPrefs.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeBytes(lastFetchDate);
// I close all streams
Loading is as follows:
FileInputStream fis = new FileInputStream("userPrefs.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
lastFetchDate=(String)ois.readObject(); //<<<<< Error thrown here
// I close all streams

You have written string as byte[] so need to read as byte[]
byte [] bString = new byte[lastFetchDate.length()*2];
ois.readFully(bString, 0, bString.length);
Or if you write as Object using writeObject method then you can read as object,
oos.writeObject(lastFetchDate);

Related

Creating File With String Object

im writing a socket program which send and receive strings.
Im trying to build a file using this received strings that are sending from other side.
this is exactly done with text files but in others such an image file doesn't work and when complete, the image does not open!
in receiver:
file = new File(dir,FileName);
fOut = new FileOutputStream(file);
dos = new DataOutputStream(fOut);
and when a msg received:
dos.write(msg.getBytes("UTF-8"));
in sender:
File file = new File(FilePath);
InputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is);
...
isr.read(inputBuffer);
I have Tried this solution but the problem didn't fixed:
Changing the UTF-8 to ISO-8859-1
using output stream writer instead of data output stream.
trying with smaller pictures than text file.
ByteArrayOutputStream is useful converting bitmap to byte array.
after then, you using BitmapFactory, decodeByteArray is converting byte to bitmap.

How to save and retrieve InputStream in text file with less overhead

I am retrieving/downloading JSON feed as InputStreamReader and I need to pass this InputStreamReader to JsonReader for parsing it.
JsonReader reader = new JsonReader(isr); // isr is the InputStreamReader instance
Everything is okay. But I want to cache this InputStreamReader or this data in some other format into internal storage for later use. For caching, I am using text file to save. (AFAIK, android default cache saves data in external storage and some devices have no external storage).
For saving this file, I am using .txt file. But I am only able to save/cache String formatted data into file and read it as String. This is the code what I have written so far to write & read file:
public static void writeObject(Context context, String key, Object object)
throws IOException {
FileOutputStream fos = context
.openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
}
public static Object readObject(Context context, String key)
throws IOException, ClassNotFoundException {
FileInputStream fis = context.openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
return object;
}
But this approach is taking too much conversion:
inputStreamReader -> inputStream -> String //to save
String -> inputStream -> inputStreamReader // retrieve
How can Save/cache this data with less overhead? Perhaps inputStreamReader needs to be converted to outputStreamReader or something like this to be writable. But I can't figure out the associative code to do this. What should be the code? Also can I cache this data by android's proposed way for later use intead of saving it as .txt file?
You can't short this process as String is best format to save information in file AFAIK. Also inputStreamReader & inputStream are not suitable object to be written. There may be other better explanatory answer :)

Does FileOutputStream is persistent after upgrading my app?

In my app I save some files using FileOutputStream class:
FileOutputStream fos;
fos = openFileOutput("my_file", Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(classToSave);
os.close();
If I upgrade my app anche the I execute:
FileInputStream fis = null;
fis = openFileInput("my_file");
ObjectInputStream is = new ObjectInputStream(fis);
myData = (MyClass) is.readObject();
is.close();
does fis is null or it contains the class that I saved before upgrade?
The file itself will be still there after the upgrade. If you can retrieve the serialised object depends on which changes you made to MyClass in the source code.

Android: Write and Get Object

I want to write a serializable object to file in internal memory. Then, I want to load that object back from that file later. How could I do this in Android?
First of all your object must implement Serializable. Don't forget to add a serialVersionUID on the serializable class.
Then if you don't want to save specific field of the object mark it as transient.
Be sure all fields are serializable.
Next create a file in the internal memory and create an ObjectOutputStream to save your object. If you want to save in a specific folder you can create a path like this:
File path=new File(getFilesDir(),"myobjects");
path.mkdir();
Then you can use that path to save your object:
File filePath =new File(path, "filename");
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
Reading is similar:
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
MyObjectClass myObject = (MyObjectClass ) in.readObject();
in.close();

Convert list in to byte[] for storing in SQLite in Android

I have this array declaration:
List<GeoPoint> race = new ArrayList<GeoPoint>();
I want to store this array in to SQLite database.
Can some one give me code for converting this array in to byte[] or give me code for another solution ?
Thanks
If your GeoPoint is Serializable you can use a ByteArrayOutputStream plus an ObjectOutputStream.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(list);
byte[] result = baos.toByteArray();
oos.close();
Resources :
ObjectOutputStream
ByteArrayOutputStream

Categories

Resources