how to read/write data dictionary in android programmatically - android

I want to read/write data dictionary in file which is in android internal/external memory. In WP7, they have used IsolatedStorage for storing the dictionary directly. In IOS, they can write NSDictionary directly to the file. Please anyone tell me the way to write DataDictionary into file.
Note: I have the keys and values in the Map variable.
how to store this Map directly to file

I would suggest putting your words into a database for the following reasons
DB lookups on android with SQLite are "fast enough" (~1ms) for even
the most impatient of users
Reading large files into memory is a dangerous practice in
memory-limited environments such as android.
Trying to read entries out of a file "in place" rather than "in
memory" is effectively trying to solve all the problems that SQLite
already solves for you.
Embed a database in the .apk of a distributed application [Android]
You can find more detailed examples by searching object serialization
[EDIT 1]
Map map = new HashMap();
map.put("1",new Integer(1));
map.put("2",new Integer(2));
map.put("3",new Integer(3));
FileOutputStream fos = new FileOutputStream("map.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(map);
oos.close();
FileInputStream fis = new FileInputStream("map.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Map anotherMap = (Map) ois.readObject();
ois.close();
System.out.println(anotherMap);
[EDIT 2]
try {
File file = new File(getFilesDir() + "/map.ser");
Map map = new HashMap();
map.put("1", new Integer(1));
map.put("2", new Integer(2));
map.put("3", new Integer(3));
Map anotherMap = null;
if (!file.exists()) {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(map);
oos.close();
System.out.println("added");
} else {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
anotherMap = (Map) ois.readObject();
ois.close();
System.out.println(anotherMap);
}
} catch (Exception e) {
}
[EDIT 3]
Iterator myVeryOwnIterator = meMap.keySet().iterator();
while(myVeryOwnIterator.hasNext()) {
String key=(String)myVeryOwnIterator.next();
String value=(String)meMap.get(key);
// check for value
}

I'm unsure if using SharedPreferences (link) is something that is suitable for your usecase.
You store via a key-value pair, and can have multiple SharedPreferences per application. While both are stored as String objects, the value can be automatically cast to other primitives using built in methods.
Mark Murphy has written a library, cwac-loaderex (link), to facilitate access of SharedPreferences via the use of the Loader pattern (link), which offsets some of the work you need to do to keep IO off the main thread.

Related

How to serialize hashmap in android

I want to serialize and deserialize very big hashmaps in android.
I'm using this code:
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(map);
objectOutputStream.close();
fileOutputStream.close();
But the output file I get is very big (300MB). How can I reduce the size?
I tried serializing a json string instead, that minimized it to about 150MB which is still way too big .

How can we use cache for stored json data in android [duplicate]

My application should work not only in online but also in offline mode. For that reason I am considering find the best way for cashing data. I't like use SharedPreference for store data but in android documentation writen Maximum size in characters allowed for a preferences value is 8192. I don't know this is ok or not? I tried to pass out of this idea trying to use FileCashing or sqLite cashing.
So what you think guys what is the best SharedPreference vs FileCashing or vs SqLiteCaching?
Save the json in cache directory as file....
Save:
// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);
// Save the JSONOvject
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( jsonObject );
out.close();
Retrieve:
// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();
I personally like to do this the following way. Create a SQLite database that can hold your content. Then, bind the user interface directly to the database using Adapters & Content Providers that send a notification whenever the data is changed so that the UI can update itself. The last piece in the equation is some form of synchronization service that downloads content and saves it to the database asynchronously. That way, you can manage your data very easily because it is all in the same place. The only part you'll have to figure out for your app is how you decide when to update or remove the data from the database.
Adapters
ContentProvider
Synchronization
Based on your requirement I would recommend SQLite data base.
Since shared preference is suitable for configuration storage - often small data/strings.
File cache is hard to manage, so I recommend SQLite - easy to manage and ability to store mass data.
Considering the performance, if the number of index is not that huge, SQLite database should have the acceptable performance. E.g. only several ms slower than a file cache.
You might be able to combine these two approaches together. Use random access file with index-offset stored in SQLite.
I have used Internal Storage which store file in Application package directory that can't be accessible by not rooted device.
Here the class which can create, read and delete the file
public class ReadWriteJsonFileUtils {
Activity activity;
Context context;
public ReadWriteJsonFileUtils(Context context) {
this.context = context;
}
public void createJsonFileData(String filename, String mJsonResponse) {
try {
File checkFile = new File(context.getApplicationInfo().dataDir + "/new_directory_name/");
if (!checkFile.exists()) {
checkFile.mkdir();
}
FileWriter file = new FileWriter(checkFile.getAbsolutePath() + "/" + filename);
file.write(mJsonResponse);
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String readJsonFileData(String filename) {
try {
File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/" + filename);
if (!f.exists()) {
onNoResult();
return null;
}
FileInputStream is = new FileInputStream(f);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
onNoResult();
return null;
}
public void deleteFile() {
File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/");
File[] files = f.listFiles();
for (File fInDir : files) {
fInDir.delete();
}
}
public void deleteFile(String fileName) {
File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/" + fileName);
if (f.exists()) {
f.delete();
}
}
}
You can create, read and delete the file by calling ReadWriteJsonFileUtils class methods as follows:
For creating file:
try {
new ReadWriteJsonFileUtils(context).createJsonFileData(file_name, data);
} catch (Exception e) {
e.printStackTrace();
}
For reading file:
String jsonString = new ReadWriteJsonFileUtils(context).readJsonFileData(file_name);
For deleting single file
new ReadWriteJsonFileUtils(context).deleteFile(file_name);
For deleting all file
new ReadWriteJsonFileUtils(context).deleteFile();

Android: ObjectInputStream throws ClassCastException when reading arraylist

I have an arraylist of a certain POJO object that implements serializable. When I write the object to disk I do it like so:
File outputDir = context.getCacheDir();
File outputFile = new File(outputDir, getCacheKey() );
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
oos.writeObject(arraylistOfMyObjects);
oos.close();
and when I later want to read back this list into an arraylist of the object, I do it like so:
File outputDir = context.getCacheDir();
File outputFile = new File(outputDir, getCacheKey() );
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(outputFile)));
final ArrayList<myObject> list = (ArrayList<myObject>) ois.readObject();
ois.close();
I'm receiving frequent exceptions on the line where I cast the inputstream to an arraylist, with the error looking like this:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.ObjectStreamClass
Why are these errors occurring and how can I change my code to resolve them?
Interestingly enough the error doesn't occur every time I run the app, but only every now and again for whatever weird reason.
Make sure you're writing and reading your objects back in the same order every time. In my case I realized I was writing to the file with optional parameters like this:
if(cacheAllItems) { // <-- BREAKS READING FILE WHEN FALSE
objectOutputStream.writeObject(itemsList);
objectOutputStream.writeObject(itemNamesList);
}
objectOutputStream.writeObject(currentItem);
Then I was attempting to read the file with:
itemsList = (ArrayList<Item>) objectInputStream.readObject(); // MIGHT NOT EXIST
itemNames = (ArrayList<String>) objectInputStream.readObject(); // MIGHT NOT EXIST
currentItem = (Item) objectInputStream.readObject();
So if I had last run my cacheItems() function with the cacheAllItems = true, everything worked fine, but if it had last run just saving currentItem, the readObject() attempted to read the one object as the list that should come first.

How to return location Object when loading file from internal file storage?

I am working on some way to store a GPS location within the internal file storage and retrieve it on demand in another method.
As I am new to Android I have tried out several ways and decided to go with the FileOutput-/InputStream as it is more comprehensible to me. I am working with the Android location API(http://developer.android.com/reference/android/location/Location.html).
I know that saving a location Object works technically via writing it to a string and later to bytes, but how can I load a saved file and have the saved location object returned?
My code approach:
public void saveCurrentLocation(){ //method works, I can see the saved file in the file explorer
Location currentLoc = gpsClass.getCurrentLocation(); //method within gpsClass that returns current location
try {
FileOutputStream fos = openFileOutput("SaveLoc", Context.MODE_PRIVATE);
fos.write(currentLoc.toString().getBytes());
fos.close();
}
catch(Exception e) { e.printStackTrace();}
}
public void loadSavedLocation() {
Location savedLoc;
try{
BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput("SaveLoc")));
String inputString;
StringBuffer stringBuffer = new StringBuffer();
while((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString);
}
gpsClass.update(??);
}
catch(Exception e) {e.printStackTrace();}
}
I'd like to pass the location Object readout in inputString to "gpsClass.update()" which only takes variables of the type Location. Do I have to make the Object serializable and if yes, how?
Many thanks in advance!
Why not persist your location objects into a SQLite database?
Or something like this:
Save:
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(your object);
os.close();
Load:
FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
YourClass yourObject = (YourClass) is.readObject();
is.close();
return yourObject;
This should get you back on track

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();

Categories

Resources