Android 配置文件

来源:互联网 发布:自学英语教材推荐知乎 编辑:程序博客网 时间:2024/05/22 00:16
public class theApp extends Application {    Context mContext ;    @Override    public void onCreate() {        super.onCreate();        mContext  = getApplicationContext();        Utils.init(mContext);    }}
public class Utils {    private static Context mContext;    public static Properties getNetConfigProperties() {        Properties props = new Properties();//        InputStream in = Utils.class.getResourceAsStream("/netconfig.properties");        Context c ;        InputStream in = null;        try {            in = mContext.getAssets().open("netconfig.properties");        } catch (IOException e) {            e.printStackTrace();        }        try {            props.load(in);        } catch (IOException e) {            e.printStackTrace();        }        return props;    }public static void init(Context context) {        mContext = context;    }}


使用的地方调用

String str = Utils.getNetConfigProperties().getProperty("name");
改进代码:这样可以将配置文件写在其他可以修改的地方,便于在apk外手动修改(我们项目中暂时
未找到打开.properties格式的工具,暂时是用U盘复制替换)
 public static Properties getNetConfigProperties() {        Log.d(TAG, "Environment="+   Environment.getExternalStorageDirectory());Properties props = new Properties();        File file = new File(Environment.getExternalStorageDirectory(),"Android/data
/bbb.properties");        try {         InputStream in = new FileInputStream(file);         props.load(in);      } catch (Exception e) {         e.printStackTrace();      }               return props;    }

0 0