请教:为什么使用Android读取SD卡根目录的T.INI配置文件,读不了?

热爱生活 2016-07-29 12:44:45

根据这个帖子:http://www.datouinfo.com/?p=1187
的方法。。
我做的一个读取SD卡根目录下面的ini配置文件内容
package com.example.ini;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
//加载输入输出库
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.os.Environment;
import java.io.File;
import java.io.IOException;

import org.dtools.ini.*;
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//插入自己的代码


boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
//sdDir = Environment.getExternalStorageDirectory();//获取跟目录

File sdCardDir = Environment.getExternalStorageDirectory();
System.out.println("sdCardDir = "+sdCardDir);
// String filename = "/sdcard/" + "T.ini";
//String filename=sdCardDir+"/"+"T.ini";
IniFile ini = new BasicIniFile(false);// 不使用大小写敏感
IniFileReader reader = new IniFileReader(ini, new File(filename));

try {
reader.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
IniSection sec_sys = ini.getSection("system");
String stationNum = sec_sys.getItem("stationNum").getValue();
System.out.println("stationNum = " + stationNum);
IniSection sec_station = ini.getSection("station");
for (IniItem item : sec_station.getItems()) {
String name=item.getName();
String value=item.getValue();
System.out.println(item.getName() + " = " + item.getValue());
}


}



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


配置文件加入了,读取许可:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

T.Ini文件内容:

#############################################
# 预警显示系统配置文件 #
#############################################
[system]
#车站数量
stationNum = 5
#上行开始公里标
upStartPost = 100
#下行开始公里标
downStartPost = 100
[station]
#编号 = 车站名,公里标,上行股道数,下行股道数
station1 = 北京西站,100.077,2,2
station2 = 天津站,110.077,2,2
station3 = 廊坊站,120.077,2,2
station4 = 德州东站,130.077,2,2
station5 = 上海虹桥站,140.077,2,2
#############################################

完整源码云盘下载:
https://yunpan.cn/c6d2845EN8gTc 访问密码 8c56

研究了好几天 都没有读取到文件 真是崩溃了!
无可奈何 周末 加班了 请大家帮忙看看吧 非常感谢!!
...全文
453 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35828876 2018-02-01
  • 打赏
  • 举报
回复
楼主最后怎么搞好了,,我也遇到这个问题了
热爱生活 2016-08-01
  • 打赏
  • 举报
回复
这里string name="系统升级了"; String aa=section.getName();//aa=系统升级了"; 但是二者的 aa 的 offset=2 hashCode=0 name offset=0 hashCode=472377986 这是我跟踪后获得的值。。。
热爱生活 2016-08-01
  • 打赏
  • 举报
回复
引用 6 楼 heaimnmn 的回复:
自己去写IniFileReader 吧,你需要做的是第一步,以File的形式读取出inputstream出来,然后保存看看事实不会编码问题,不是继续去解析inputstream就ok了
感谢 已经解决了 首先 ini文件编码格式要改成UTF-8格式的,然后,源码里面的ASCII全部修改成UTF-8,最好 里面的校验要全面去掉 否则验证不通过。。。比较费事。。。 现在出现了另外一个问题: public IniSection getSection(String name)//name="系统升级了" { for (IniSection section : getSections()) { String aa=section.getName();//aa offset=2 name offset=0 if(aa.equals(name))//aa.equals(name) ==false { //进入不了 } } offset值不同导致相同的字符串比较也是false 怎么办?如何去除offset值的干扰? 请大家指教 谢谢!
哎,真难 2016-08-01
  • 打赏
  • 举报
回复
自己去写IniFileReader 吧,你需要做的是第一步,以File的形式读取出inputstream出来,然后保存看看事实不会编码问题,不是继续去解析inputstream就ok了
maneaterliang 2016-07-31
  • 打赏
  • 举报
回复
应该是你配置文件的编码格式不对,需要修改成UTF8
热爱生活 2016-07-31
  • 打赏
  • 举报
回复
感谢回复! 不是文件编码的问题 是读取编码的问题 读取编码格式需要修改成UTF-8我已经再源码中修改了
热爱生活 2016-07-30
  • 打赏
  • 举报
回复
之所以不支持中文,网上看到有高手说是读取的编码是用ASCII的,需要改成UTF-8格式的 我把jar的文件 IniFileReader.class 变成了IniFileReader.java文件 修改ASCII,改成UTF-8 内容如下:
/*     */ package org.dtools.ini;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ 
/*     */ public class IniFileReader
/*     */ {
/*     */   private File file;
/*     */   private IniFile ini;
/*     */ 
/*     */   static String getEndLineComment(String line)
/*     */   {
/*  52 */     if ((!isSection(line)) && (!isItem(line))) {
/*  53 */       throw new FormatException("getEndLineComment(String) is unable to return the comment from the given string (\"" + 
/*  55 */         line + "\" as it is not an item nor a section.");
/*     */     }
/*     */ 
/*  58 */     int pos = line.indexOf(';');
/*     */ 
/*  60 */     if (pos == -1) {
/*  61 */       return "";
/*     */     }
/*     */ 
/*  64 */     return line.substring(pos + 1).trim();
/*     */   }
/*     */ 
/*     */   static String getItemName(String line)
/*     */   {
/*  82 */     if (!isItem(line)) {
/*  83 */       throw new FormatException("getItemName(String) is unable to return the name of the item as the given string (\"" + 
/*  85 */         line + "\" is not an item.");
/*     */     }
/*     */ 
/*  89 */     int pos = line.indexOf('=');
/*     */ 
/*  92 */     if (pos == -1) {
/*  93 */       return "";
/*     */     }
/*     */ 
/*  96 */     return line.substring(0, pos).trim();
/*     */   }
/*     */ 
/*     */   static String getItemValue(String line)
/*     */   {
.......
/* 379 */     BufferedReader reader = new BufferedReader(
/* 380 */       new InputStreamReader(
/* 381 */       new FileInputStream(this.file), "UTF-8"));//在这里修改的。。。。。。原来是ASCII
/*     */ 
........................
/* 542 */     reader.close();
/*     */   }
/*     */ }

/* Location:           E:\development\sourcecode\android\ini读取jar包\org.dtools.javaini-v1.1.00.jar
 * Qualified Name:     org.dtools.ini.IniFileReader
 * JD-Core Version:    0.6.2
 */
然后 我需要把java 我需要把IniFileReader.java文件编译成IniFileReader.class文件 使用 javac IniFileReader.java 结果编译出错了 : 内容如下: >javac IniFileReader1.java IniFileReader1.java:9: 错误: 类IniFileReader是公共的, 应在名为 IniFileReader.jav a 的文件中声明 /* */ public class IniFileReader ^ IniFileReader1.java:12: 错误: 找不到符号 /* */ private IniFile ini; ^ 符号: 类 IniFile 位置: 类 IniFileReader IniFileReader1.java:140: 错误: 找不到符号 /* */ public IniFileReader(IniFile ini, File file) ^ 符号: 类 IniFile 位置: 类 IniFileReader IniFileReader1.java:17: 错误: 找不到符号 /* 53 */ throw new FormatException("getEndLineComment(String) is unable t o return the comment from the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader1.java:33: 错误: 找不到符号 /* 83 */ throw new FormatException("getItemName(String) is unable to retu rn the name of the item as the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader1.java:49: 错误: 找不到符号 /* 115 */ throw new FormatException("getItemValue(String) is unable to ret urn the value of the item as the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader1.java:75: 错误: 找不到符号 /* 165 */ throw new FormatException("getSectionName(String) is unable to r eturn the name of the section as the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader1.java:158: 错误: 找不到符号 /* 374 */ IniSection currentSection = null; ^ 符号: 类 IniSection 位置: 类 IniFileReader IniFileReader1.java:165: 错误: 找不到符号 /* 388 */ Commentable lastCommentable = null; ^ 符号: 类 Commentable 位置: 类 IniFileReader IniFileReader1.java:169: 错误: 已在方法 read()中定义了变量 line /* 395 */ String line = line.trim(); ^ IniFileReader1.java:217: 错误: 找不到符号 /* 482 */ throw new FormatException("An Item has been read,before any section."); ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader1.java:223: 错误: 找不到符号 /* */ IniItem item; ^ 符号: 类 IniItem 位置: 类 IniFileReader IniFileReader1.java:233: 错误: 找不到符号 /* */ catch (InvalidNameException e) ^ 符号: 类 InvalidNameException 位置: 类 IniFileReader IniFileReader1.java:235: 错误: 找不到符号 /* */ IniItem item; ^ 符号: 类 IniItem 位置: 类 IniFileReader IniFileReader1.java:236: 错误: 找不到符号 /* 506 */ throw new FormatException("The string \"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader1.java:241: 错误: 找不到符号 /* */ IniItem item; ^ 符号: 类 IniItem 位置: 类 IniFileReader 16 个错误 E:\development\sourcecode\android\ini读取jar包>javac IniFileReader.java IniFileReader.java:12: 错误: 找不到符号 private IniFile ini; ^ 符号: 类 IniFile 位置: 类 IniFileReader IniFileReader.java:139: 错误: 找不到符号 public IniFileReader(IniFile ini, File file) ^ 符号: 类 IniFile 位置: 类 IniFileReader IniFileReader.java:17: 错误: 找不到符号 throw new FormatException("getEndLineComment(String) is unable to return t he comment from the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader.java:32: 错误: 找不到符号 throw new FormatException("getItemName(String) is unable to return the nam e of the item as the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader.java:48: 错误: 找不到符号 throw new FormatException("getItemValue(String) is unable to return the va lue of the item as the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader.java:74: 错误: 找不到符号 throw new FormatException("getSectionName(String) is unable to return the name of the section as the given string (\"" + ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader.java:157: 错误: 找不到符号 IniSection currentSection = null; ^ 符号: 类 IniSection 位置: 类 IniFileReader IniFileReader.java:164: 错误: 找不到符号 Commentable lastCommentable = null; ^ 符号: 类 Commentable 位置: 类 IniFileReader IniFileReader.java:168: 错误: 已在方法 read()中定义了变量 line String line = line.trim(); ^ IniFileReader.java:215: 错误: 找不到符号 throw new FormatException("An Item has been read,before any section." ); ^ 符号: 类 FormatException 位置: 类 IniFileReader IniFileReader.java:221: 错误: 找不到符号 IniItem item; ^ 符号: 类 IniItem 位置: 类 IniFileReader IniFileReader.java:231: 错误: 找不到符号 catch (InvalidNameException e) ^ 符号: 类 InvalidNameException 位置: 类 IniFileReader IniFileReader.java:233: 错误: 找不到符号 IniItem item; ^ ............ 15 个错误 彻底蒙了。。。。。。怎么回事 ? 请高手帮忙指教一下非常感谢!这个对我来说很重要!在线等等回复!!睡不着 万分感谢!! 文件我发到网盘了: https://yunpan.cn/c6H4SfFcBJtye (提取码:d5fc)
热爱生活 2016-07-30
  • 打赏
  • 举报
回复
感谢您的回复! 通过文件判断函数,判断文件是存在的! try { reader.read();//调试到这句话 程序崩溃 } catch (IOException e) { e.printStackTrace(); } 我昨晚我做梦 都梦到这个程序了 我今早看到您的回复 灵机一动 把配置文件里面的####去掉 跟踪调试 发现可以读取文件内容了 可是 中文部分读取的是乱码!! [system] stationNum = 5 upStartPost = 100 downStartPost = 100 [station] station1 = 北京西站,100.077,2,2 station2 = 天津站,110.077,2,2 station3 = 廊坊站,120.077,2,2 station4 = 德州东站,130.077,2,2 station5 = 上海虹桥站,140.077,2,2
Birds2018 2016-07-29
  • 打赏
  • 举报
回复
你说读不了有没有先判断文件是否存在 然后在读取流,或者有什么错误没。

80,359

社区成员

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

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