日期:2014-05-20  浏览次数:20753 次

求教 文件读取到最后一行时总是报错。麻烦高手详细解释或更正
/**
 * 
 * <DL>
 * <DT><B>系统配置文件读取类</B></DT>
 * </DL>
 * 处理具有类似下面内容的文件:
[System Define]
SYSID=006
SYSNAME=支票影像交换系统
Authenticated MODE=1
Server Access MODE= CLIENT
IPADD=
URL=
CLIENT= PATH
AB=

[System Define]
SYSID=007
SYSNAME=电子验印系统-联机
Authenticated MODE=1
Server Access MODE= CLIENT
IPADD=
URL=
CLIENT= PATH
AB=

 * @author
 * @author
 * @version $Revision: 1.00 $ $Date: 2011/10/22 $
 */
public class ConfigFileUtil{
private static String instanceDir;
private static final String FILE_NAME = "/resources/para/uccb.dat";
private static final String FILE_NAME2 = "/resources/para/UCCB_field.hlp";
/***
* 读取系统配置文件
* @param trade
* @return
*/
public static Map<String, String> readConfigFile(Trade trade, String sysId){
Map<String, String> m = new HashMap<String, String>();
m.put("sign", "");
String instanceDir = getInstanceDir();// /home/abs/workspace
String projectDir = trade.getResourceManager().getProjectName(
trade.getClass().getName());
String fileName = instanceDir + "/" + projectDir + FILE_NAME;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line = "";
String mark = "0";
while ((line = br.readLine().trim()) != null) {
if (line.length() == 0){
continue;
}
read(line, m);
String[] str = line.split("=");
if ("SYSID".equals(str[0])){
if (str[1].equals(sysId)){
mark = "1";
continue;
}
}
if ("[System Define]".equals(line)){
if (!"1".equals(mark)){
m.clear();
continue;
} else {
mark = "0";
break;
}
}
}
br.close();
} catch (FileNotFoundException e) {
trade.showInfo("单点登录系统配置文件不存在:" + FILE_NAME);
} catch (IOException e) {
trade.showInfo("读取单点登录系统配置文件[ " + FILE_NAME + " ]失败!");
} catch (NullPointerException e){
m.put("sign", "over");
} catch (Exception e){
}
return m;
}
   
 
/***
* 分割键值
* @param line
* @return
*/
private static void read(String line, Map<String, String> map) throws IOException{
if ("".equals(line.trim())){
return ;
}
int index = line.indexOf("=");
if (index < 0){
return ;
}
String key = line.substring(0, index);
if (index + 1 == line.length()){
map.put(key, "");
} else {
String value = line.substring(index+1);
map.put(key, value);
}
return ;
}




------解决方案--------------------
while ((line = br.readLine().trim()) != null) {

文件结尾是 readLine 返回 null,怎么调用 trim 方法。

trim调用放到循环开始,不要放到while条件判断里。