Store a small data cache

Put your problem here if it does not fit any of the other categories.

Store a small data cache

Postby Skeniver » Tue Dec 15, 2009 11:50 am

Hi there

I have been trying to store a small amount of data in a cache/file for my application. It's not worth doing in a database, but also don't want my app reply on the user having a SD card.

This is the code I have at the moment:
Write the data to file:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                                         try {
  3.  
  4.                                                 FileOutputStream fOut = cont.openFileOutput("categories.txt", Context.MODE_PRIVATE);
  5.  
  6.                                                 OutputStreamWriter osw = new OutputStreamWriter(fOut);
  7.  
  8.                                                 for (int x = 0; x < Global.categories.size(); x++) {
  9.  
  10.                                                         osw.write(Global.categories.get(x).name + "," + Global.categories.get(x).id);
  11.  
  12.                                                 }
  13.  
  14.                                                 osw.flush();
  15.  
  16.                                                 osw.close();
  17.  
  18.                                                 Global.loadedCategories = true;
  19.  
  20.                                                 if (progress != null && progress.isShowing())
  21.  
  22.                                                         progress.dismiss();
  23.  
  24.                                         } catch (Exception e) { }
  25.  
  26.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


And then to try read the file:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         try {
  3.  
  4.                 File f = new File("categories.txt");
  5.  
  6.                 BufferedReader rdr = new BufferedReader(new FileReader(f));
  7.  
  8.                
  9.  
  10.                 try {
  11.  
  12.                         String test;
  13.  
  14.                         do {
  15.  
  16.                                 test = rdr.readLine();
  17.  
  18.                                 int t = 1;
  19.  
  20.                         } while (test != null);
  21.  
  22.                 } catch (Exception e) {}
  23.  
  24.                
  25.  
  26.         } catch (Exception e) {}
  27.  
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


But the file is never found to be read... I have tried to search for a solution, but am coming up short. Any help would be much appreciated!

Thanks in advance
Neil
Follow me on twitter -> http://twitter.com/Skeniver
Apps & blog -> http://www.formidableapps.co.uk
User avatar
Skeniver
Senior Developer
Senior Developer
 
Posts: 106
Joined: Tue Feb 03, 2009 3:51 pm
Location: Canterbury, UK

Top

Postby wiederke » Tue Dec 15, 2009 1:48 pm

Hi,

if you use Context.openFileOutput the file is created in the application's context. Use Context.openFileInput to read it from the context again.

Read also this tutorial working_with_files-t115.html


Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby wiederke » Tue Dec 15, 2009 1:50 pm

Hi,

if you just store a small amount of data Preferences may work for you.
Read this: http://developer.android.com/guide/topi ... .html#pref


Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby Skeniver » Tue Dec 15, 2009 3:08 pm

Hey wiederke

Thanks for replying. Does the Context.openFileOutput store data on the phone or on an SD card?

Preferences are not quite what I need as I need to store an id and a name (although it could be used)

Thanks again!
Neil
Follow me on twitter -> http://twitter.com/Skeniver
Apps & blog -> http://www.formidableapps.co.uk
User avatar
Skeniver
Senior Developer
Senior Developer
 
Posts: 106
Joined: Tue Feb 03, 2009 3:51 pm
Location: Canterbury, UK

Postby wiederke » Tue Dec 15, 2009 8:32 pm

Hi,

Context.openFileOutput stores the file on the phone.


Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 9 guests