visited 13 states (5.77%)
Create your own visited map of The World or Triposo world travel guide for Android

2014-10-03

Android應用程式中INI file之操作

一般電腦應用程式的開發,常常都會將一些可供調整又常用的參數,放置於*.ini檔案中,用來設定程式初始化時所需的條件,也常常可供修改,將新的設定寫入*.ini檔案中,特別整理一些常用關於*.ini檔案讀寫的流程。




import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author Ananti
 */
public class Config {

    private Properties configuration;
    private String configurationFile = "config.ini";

    public Config() {
        configuration = new Properties();
    }

    public boolean load() {
        boolean retval = false;

        try {
            configuration.load(new FileInputStream(this.configurationFile));
            retval = true;
        } catch (IOException e) {
            System.out.println("Configuration error: " + e.getMessage());
        }

        return retval;
    }

    public boolean store() {
        boolean retval = false;

        try {
            configuration.store(new FileOutputStream(this.configurationFile), null);
            retval = true;
        } catch (IOException e) {
            System.out.println("Configuration error: " + e.getMessage());
        }

        return retval;
    }

    public void set(String key, String value) {
        configuration.setProperty(key, value);
    }

    public String get(String key) {
        return configuration.getProperty(key);
    }

    public void list(){
        configuration.propertyNames;
    }
}
如果要跟電腦上程式去讀寫*.ini檔案一樣可以區分出不同的section,
ex: [test]
     test=1
     test=2
可以考慮用以下的方式去處理
//ini file without section

import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStream;  
import java.util.Properties;  
import android.content.Context;  
  
public class IniReaderNoSection {  
    public Properties properties = null;  
  
    
//If ini file has section
import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStream;  
import java.util.Properties;  
import android.content.Context; 

    public IniReaderNoSection(Context context, int resourceId) {  
        InputStream inputStream = context.getResources().openRawResource(  
                resourceId);  
        try {  
            properties = new Properties();  
            properties.load(inputStream);  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
  
    /** 
     * ファイルのアドレス---->例えば、SD存储卡 
     *  
     */  
    public IniReaderNoSection(String filename) {  
        File file = new File(filename);  
        try {  
            properties = new Properties();  
            properties.load(new FileInputStream(file));  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
  
    public String getIniKey(String key) {  
        if (properties.containsKey(key) == false) {  
            return null;  
        }  
        return String.valueOf(properties.get(key));  
    }  
}

//ini file with section

import java.io.BufferedReader;  
import java.io.FileReader;  
import java.io.IOException;  
import java.util.HashMap;  
import java.util.Map;  
import java.util.Properties;  
  
public class IniReaderHasSection {  
    private Map sections;  
    private String secion;  
    private Properties properties;  
  
    /** 
     * ファイルのアドレス---->例えば、SD存储卡 
     *  
     */  
    public IniReaderHasSection(String filename) throws IOException {  
        sections = new HashMap();  
        BufferedReader reader = new BufferedReader(new FileReader(filename));  
        read(reader);  
        reader.close();  
    }  
  
    private void read(BufferedReader reader) throws IOException {  
        String line;  
        while ((line = reader.readLine()) != null) {  
            parseLine(line);  
        }  
    }  
  
    private void parseLine(String line) {  
        line = line.trim();  
        if (line.matches("\\[.*\\]") == true) {  
            secion = line.replaceFirst("\\[(.*)\\]", "$1");  
            properties = new Properties();  
            sections.put(secion, properties);  
        } else if (line.matches(".*=.*") == true) {  
            if (properties != null) {  
                int i = line.indexOf('=');  
                String name = line.substring(0, i);  
                String value = line.substring(i + 1);  
                properties.setProperty(name, value);  
            }  
        }  
    }  
  
    public String getValue(String section, String name) {  
        Properties p = sections.get(section);  
  
        if (p == null) {  
            return null;  
        }  
  
        String value = p.getProperty(name);  
        return value;  
    }  
  
}

這些也是同標籤文章 :

沒有留言: