packagecn.ycmedia.common.utils;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStreamReader;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importcom.qiniu.http.Response;public classReadTxt {static JSONObject jsxxsx = newJSONObject();public static voidreadTxtFile(String filePath){try{
String encoding="utf-8";
File file=newFile(filePath);
JSONObject json= newJSONObject();if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = newInputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = newBufferedReader(read);
String lineTxt= null;
JSONObject js= null;while((lineTxt = bufferedReader.readLine()) != null){
js=JSONObject.parseObject(lineTxt);
JSONArray jsAr= js.getJSONArray("features");for (int i = 0; i < jsAr.size(); i++) {
JSONObject jsP=(JSONObject)jsAr.get(i);
JSONObject xx=jsP.getJSONObject("properties");
String city=xx.getString("name");
Double[] cp=xx.getObject("cp", Double[].class);
jsxxsx.put(city, cp);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}public static void main(String argv[]) throwsException{
showAllFiles(new File("D:\\test\\111"));
System.err.println(jsxxsx);
}public static void showAllFiles(File dir) throwsException {
File[] fs=dir.listFiles();for (int i = 0; i < fs.length; i++) {if(fs[i].isDirectory()) {try{
showAllFiles(fs[i]);
}catch(Exception e) {
}
}else{//打印返回的信息
readTxtFile(fs[i].getAbsolutePath());
}
}
}
}