public class IniReader { //用于存放配置文件的属性值
protected HashMap<String, Properties> sections = new HashMap<String, Properties>();
private transient String currtionSecion;
private transient Properties current; /**
* 读取文件
* @param filename 文件名
* @throws IOException
*/
public IniReader(String name, Context context) throws IOException
{
InputStream in = context.getAssets().open(name);
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
BufferedReader read = null;
try {
if(reader != null)
{
read = new BufferedReader(reader);
reader(read);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new FileNotFoundException("文件不存在或者文件读取失败");
}
} /**
* 设置每次读取文件一行
* @param reader 文件流
* @throws IOException
*/
private void reader(BufferedReader reader) throws IOException {
// TODO Auto-generated method stub
String line = null;
try {
while((line = reader.readLine()) != null)
{
parseLine(line);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new IOException("文件内容读取失败");
}
} /**
* 获取ini文件的属性值
* @param line ini文件每行数据
*/
private void parseLine(String line) {
// TODO Auto-generated method stub
try {
if (line != null) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
currtionSecion = line.replaceFirst("\\[(.*)\\]", "$1");
current = new Properties();
sections.put(currtionSecion, current);
} else if (line.matches(".*=.*")) {
if (current != null) {
int i = line.indexOf('=');
String name = line.substring(0, i-1); String value = line.substring(i+2,i+3); current.setProperty(name, value); }
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 用于获取属性值的值
* @param section 整体属性的值
* @param name 属性值名字
* @return 属性值的值
*/
public String getValue(String section, String name)
{ Properties p = (Properties)sections.get(section); if(p == null)
{
return null;
}
String value = p.getProperty(name); return value;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (TextView) findViewById(R.id.test);
try {
String name = new IniReader("/sdcard/SysSet.ini", MainActivity.this).getValue("StatusSet", "ShowMode");
test.setText(name);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
SysSet.ini文件测试内容
[StatusSet]
ShowMode = 0;
SearchScope = 1;
LocationMode = 0;
[InfoSet]
SoundWarn = 0;
QuakeWarn = 0;
NotificationBarShow = 0;
protected HashMap<String, Properties> sections = new HashMap<String, Properties>();
private transient String currtionSecion;
private transient Properties current; /**
* 读取文件
* @param filename 文件名
* @throws IOException
*/
public IniReader(String name, Context context) throws IOException
{
InputStream in = context.getAssets().open(name);
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
BufferedReader read = null;
try {
if(reader != null)
{
read = new BufferedReader(reader);
reader(read);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new FileNotFoundException("文件不存在或者文件读取失败");
}
} /**
* 设置每次读取文件一行
* @param reader 文件流
* @throws IOException
*/
private void reader(BufferedReader reader) throws IOException {
// TODO Auto-generated method stub
String line = null;
try {
while((line = reader.readLine()) != null)
{
parseLine(line);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new IOException("文件内容读取失败");
}
} /**
* 获取ini文件的属性值
* @param line ini文件每行数据
*/
private void parseLine(String line) {
// TODO Auto-generated method stub
try {
if (line != null) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
currtionSecion = line.replaceFirst("\\[(.*)\\]", "$1");
current = new Properties();
sections.put(currtionSecion, current);
} else if (line.matches(".*=.*")) {
if (current != null) {
int i = line.indexOf('=');
String name = line.substring(0, i-1); String value = line.substring(i+2,i+3); current.setProperty(name, value); }
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 用于获取属性值的值
* @param section 整体属性的值
* @param name 属性值名字
* @return 属性值的值
*/
public String getValue(String section, String name)
{ Properties p = (Properties)sections.get(section); if(p == null)
{
return null;
}
String value = p.getProperty(name); return value;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (TextView) findViewById(R.id.test);
try {
String name = new IniReader("/sdcard/SysSet.ini", MainActivity.this).getValue("StatusSet", "ShowMode");
test.setText(name);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
SysSet.ini文件测试内容
[StatusSet]
ShowMode = 0;
SearchScope = 1;
LocationMode = 0;
[InfoSet]
SoundWarn = 0;
QuakeWarn = 0;
NotificationBarShow = 0;
解决方案 »
- 我一运行就报错,大神帮我看看
- 有没有android SDK中文文档
- android 模拟器获取网络数据
- 帮我看看哪错了?logcat中提示这个出错了
- 求UI组件名称
- android里的TimerTask
- 优化ListView滑动的效果
- eclipse AVD启动问题
- Android Studio跳转时遇到 IllegalArgumentException: No view found for id
- 安卓第三方字体在真机上显示不出来
- 求助,一个运行时出现的错误,5.0以下版本的模拟器不报错,7.0的真机和模拟器也不报错,5.0版本的模拟器和5.1.1的真机报错,并没有指定错误的位置
- 安卓自定义View问题 思路
SD上的要自己openFile读