大神,,求救!读取不了sd卡ini文件
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;