android中记要、读取程序的配置信息

android中记录、读取程序的配置信息

写入配置信息

private static void writeConfiguration(Context context,
			LocaleConfiguration configuration) {
		DataOutputStream out = null;
		try {
			out = new DataOutputStream(context.openFileOutput(PREFERENCES,
					MODE_PRIVATE));
			out.writeUTF(configuration.locale);
			out.writeInt(configuration.mcc);
			out.writeInt(configuration.mnc);
			out.flush();
		} catch (FileNotFoundException e) {
			// Ignore
		} catch (IOException e) {
			// noinspection ResultOfMethodCallIgnored
			context.getFileStreamPath(PREFERENCES).delete();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// Ignore
				}
			}
		}
	}

 2,读去程序配置信息

private static void readConfiguration(Context context,
			LocaleConfiguration configuration) {
		DataInputStream in = null;
		try {
			in = new DataInputStream(context.openFileInput(PREFERENCES));
			configuration.locale = in.readUTF();
			configuration.mcc = in.readInt();
			configuration.mnc = in.readInt();
		} catch (FileNotFoundException e) {
			// Ignore
		} catch (IOException e) {
			// Ignore
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// Ignore
				}
			}
		}
	}

 3,

private static class LocaleConfiguration {
		public String locale;
		public int mcc = -1;
		public int mnc = -1;
	}
 

 

注意:

 PREFERENCES代表的是String 类型的一个记录信息的文件名:

 

private static final String PREFERENCES = "launcher.preferences";
1 楼 yutinglong 2011-08-10  
帅哥!你知道launcher.preferences文件存储在哪么?是在那个目录下?是会一直存在还是经过这段代码创建后才存在的!
2 楼 yangguangfu 2011-08-10  
yutinglong 写道
帅哥!你知道launcher.preferences文件存储在哪么?是在那个目录下?是会一直存在还是经过这段代码创建后才存在的!

/data/data/com.android.launcher