一、配置文件
比如 Python ,支持 .ini 文件。
在 Java 中, 支持的是 .properties 文件的读写。
JDK 内置的 java.util.Properties 类为我们操作 .properties 文件提供了便利。
Properties类
导入Java包
import java.util.Properties;
//↓参考资料
https://www.cnblogs.com/bakari/p/3562244.html
使用它的两个方法
//↓参考资料
http://blog.csdn.net/jdfkldjlkjdl/article/details/46831219#comments
1.
./**
* 读取配置文件
*/
public static Properties loadConfig(Context context, String file) {...}
2.
/**
* 保存配置文件
* /
public static boolean saveConfig(Context context, String file, Properties properties) {...}
3测试
prop.put(“类型名称”,“存入值”);// put方法可以直接修改配置信息
Prop.get(“类型名称”,“取出值”);// get出来的都是Object对象
附:代码1
二、在Edittext中传数据
测试用layout
<EditText
<Button
1.对EditText设置监听
mEditText.setOnKeyListener(listener) ;
取出输入数据:
mEditText.getText().toString();
测试:
//↓参考资料
http://blog.sina.com.cn/s/blog_883d8fb80100xsa5.html
- 点击确认,将数据传入配置文件
btn1.setOnClickListener(new View.OnClickListener() {...}
附:代码2
代码1:
/**
* 读取配置文件
* <p>
* Title: loadConfig
* <p>
* <p>
* Description:
* </p>
*
* @param context
* @param file
* @return
*/
public static Properties loadConfig(Context context, String file) {
Properties properties = new Properties();
try {
FileInputStream s = new FileInputStream(file);
properties.load(s);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return properties;
}
/**
* 保存配置文件
* <p>
* Title: saveConfig
* <p>
* <p>
* Description:
* </p>
*
* @param context
* @param file
* @param properties
* @return
*/
public static boolean saveConfig(Context context, String file, Properties properties) {
try {
File fil = new File(file);
if (!fil.exists())
fil.createNewFile();
FileOutputStream s = new FileOutputStream(fil);
properties.store(s, "");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public void TestProp() {
prop = loadConfig(context,"/mnt/sdcard/sconfig.properties" );
if (prop == null) {
// 配置文件不存在的时候创建配置文件 初始化配置信息
prop = new Properties();
prop.put("bool", "ture");
prop.put("string", "settings_config_test");
prop.put("int", "110");// 也可以添加基本类型数据 get时就需要强制转换成封装类型
saveConfig(context, "/mnt/sdcard/sconfig.properties", prop);
}
prop.put("bool", "no");// put方法可以直接修改配置信息,不会重复添加
boolean b = !(((String) prop.get("bool")).equals("ture")) ;// get出来的都是Object对象
// 如果是基本类型需要用到封装类
Log.i(TAG, " config: " + b);
String s = (String) prop.get("string");
Log.i(TAG, " config: " + s);
int i = Integer.parseInt((String) prop.get("int"));
Log.i(TAG, " config: " + i);
saveConfig(context, "/mnt/sdcard/sconfig.properties", prop);
}
代码2:
①Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/setting_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText_config"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="请输入阈值:" />
<Button
android:id="@+id/button_config"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
</LinearLayout>
②
public Properties prop;
private Context context;
protected static final String TAG="settings_config";
private EditText mEditText;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thrid);
TestProp();
mEditText = (EditText) findViewById(R.id.editText_config);
btn1 = (Button) findViewById(R.id.button_config);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast tst = Toast.makeText(ThirdActivity.this, "配置成功", Toast.LENGTH_SHORT);
tst.show();
String conf = mEditText.getText().toString();
prop.put("string",conf);
Log.i(TAG, " config: " + conf);
}
});
}