大神,,求救!读取不了sd卡ini文件

qq_35828876 2018-02-01 03:06:43
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;
...全文
542 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
UESTC少尉 2018-02-06
  • 打赏
  • 举报
回复
context.getAssets().open(name),替换一下吧
jklwan 2018-02-05
  • 打赏
  • 举报
回复
getAssets读取的是apk包中assets下的文件,你读取sd卡内的文件肯定不行。
  • 打赏
  • 举报
回复
一楼说的对 SD上的要自己openFile读

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧