Using Ini file to manage Preferences : File « File « Android






Using Ini file to manage Preferences

    
//package com.a4studio.android.newsreader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;

import android.content.Context;
import android.util.Log;

public class Preferences {
  
  private final static String TAG=Preferences.class.getSimpleName();
  
  private Context context;
  
  public final static String PREF_FILE_NAME="prefs.ini";
  public final static String TIME_KEY="time";
  public final static String KEYWORD_KEY="keyword";
  public final static String RESULT_NUM_KEY="result";
  
  private Properties props;
  
  public Preferences( Context context)
  {
    this.context = context;    

    boolean isExist =true;
    try {
      context.openFileInput(PREF_FILE_NAME);
    } catch (FileNotFoundException e) {
      Log.e(TAG, "can't read pref file", e);
      isExist = false;
    }
    props = new Properties();
    if(!isExist)
    {
      reconstruct();
    }
    props.clear();
    
    FileInputStream fin;
    try {
      fin = context.openFileInput(PREF_FILE_NAME);
      props.load(fin);
      fin.close();
    } catch (FileNotFoundException e) {
      Log.e(TAG, "can't read pref file", e);
    } catch (IOException e) {
      Log.e(TAG, "can't read pref file", e);
    }
  }
  
  public int getResultNumSetting()
  {
    String time =  props.getProperty(RESULT_NUM_KEY, "1");
    int timeValue;
    try
    {
      timeValue = Integer.parseInt(time);
    }
    catch(Exception e)
    {
      //the file could be crashed, so reconstruct one more
      reconstruct();
      time =  props.getProperty(RESULT_NUM_KEY, "1");
      timeValue = Integer.parseInt(time);
    }
    
    return timeValue;
    
  }
  
  public long getTimeSetting()
  {
    String time =  props.getProperty(TIME_KEY, "0");
    long timeValue;
    try
    {
      timeValue = Long.parseLong(time);
    }
    catch(Exception e)
    {
      reconstruct();
      time =  props.getProperty(TIME_KEY, "0");
      timeValue = Long.parseLong(time);
    }
    
    return timeValue;
    
  }
  
  public String getKeywordSetting()
  {
    return props.getProperty(KEYWORD_KEY, "science");
  }
  
  public void setTimeSetting(long time)
  {
    try {
      Log.d(TAG, "setTimeSetting");
      FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
      props.setProperty(TIME_KEY, time+"");
      props.store(fos, "a4studio");
      fos.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't read pref file", e);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't read pref file", e);
    }
  }
  
  public void setKeywordSetting(String keyword)
  {
    try {
      Log.d(TAG, "setKeywordSetting");
      FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
      props.setProperty(KEYWORD_KEY, keyword);
      props.store(fos, "a4studio");
      fos.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't read pref file", e);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't read pref file", e);
    }
  }
  
  public void setResultNumSetting(int result)
  {
    try {
      Log.d(TAG, "setResultNumSetting");
      FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
      props.setProperty(RESULT_NUM_KEY, result+"");
      props.store(fos, "a4studio");
      fos.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't read pref file", e);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't read pref file", e);
    }
  }
  
  private void reconstruct()
  {
    try {
      Log.d(TAG, "reconstruct");
      FileOutputStream fos = null;
      fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
/*      props.setProperty(TIME_KEY, System.currentTimeMillis()+"");
      props.setProperty(KEYWORD_KEY, "science");
      props.store(fos, "reconstrut version");*/
      fos.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't create pref file", e);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "can't create pref file", e);
    }
  }

}

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Save to Android local file system
10.File Cache
11.Format File Size
12.File Upload
13.File Name Extension Utils
14.Write and append string to file
15.Transfers required files to the the private file system part in order to be access them from C Code.
16.returns a canonical line of a obj or mtl file.
17.Trims down obj files, so that they may be parsed faster later on.
18.Format File Size:KB, MB, GB
19.Get File Contents as String
20.Read File and return CharSequence
21.Copy File
22.Read/write From FileSystem, browse Account
23.Create Path and file under External Storage
24.Get File Extension Name
25.Extract File Name From String
26.Write String to file
27.Create an output file from raw resources.
28.Copies a directory from a jar file to an external directory.
29.Reads a file from /raw/res/ and returns it as a String
30.Read Config ini file
31.Read Write File Manager
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.