JAVA获取文件数据 ( xxxxx.json )

    //路径fPix
File filePath = new File(fPix);
System.out.print("文件路径:" + filePath);
try {
if (filePath.isFile() && filePath.exists()) { //判断文件是否存在
PageData temp = null;
InputStreamReader read = new InputStreamReader(
new FileInputStream(filePath), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
String code = "";
while ((lineTxt = bufferedReader.readLine()) != null) {
try {
String[] linetemp = null;
lineTxt = lineTxt.trim().replace("\uFEFF", "");
lineTxt = lineTxt.substring(1, lineTxt.length() - 1);
lineTxt = lineTxt.replaceAll("'", "");
System.out.println("去除:" + lineTxt);//读取文件内容
temp = new PageData();
if (!lineTxt.equals("") && lineTxt.indexOf(", ") != -1) {
linetemp = lineTxt.split(", ");
System.out.println(linetemp);
if (linetemp != null && linetemp.length > 0) {
for (int j = 0; j < linetemp.length; j++) {
code = linetemp[j].substring(1, linetemp[j].indexOf(":") - 1).trim();
System.out.println("code:" + code);
if (code.equals("code") || code.equals("time")) {
if (temp != null) {
temp.put(code, linetemp[j].substring(linetemp[j].indexOf(":") + 2, linetemp[j].length()).trim());
System.out.println("取出数据:" + temp);
}
}
}
}
}
filelist.add(temp);
} catch (Exception e) {
System.out.println("处理本行数据出错:" + lineTxt.toString() + " 错误是:" + e.toString());
filelist.remove(temp);
continue;
}
}
read.close();
} else {
System.out.println("找不到指定的文件");
}


} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
posted @ 2017-04-18 17:59  国强则无惧!  阅读(881)  评论(0编辑  收藏  举报