Java处理文件小例子--获取全国所有城市的坐标

需求:前端展示数据,全国城市的坐标

这个是 echarts 的 制作地图JSON 工具, 但是没有全国的,只有各个省

前端需要的数据:   

所以现在 下载所有的省数据:

然后是Java处理。 这是这里重点, 在开发中, 很多繁琐的事 ,我们都可以用程序来控制:

package cn.ycmedia.common.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.http.Response;

public class ReadTxt {
    
    static JSONObject jsxxsx = new JSONObject();
      public static void readTxtFile(String filePath){
            try {
                    String encoding="utf-8";
                    File file=new File(filePath);
                    JSONObject json = new JSONObject();
                    if(file.isFile() && file.exists()){ //判断文件是否存在
                        InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                        BufferedReader bufferedReader = new BufferedReader(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[]) throws Exception{
            showAllFiles(new File("D:\\test\\111"));
             System.err.println(jsxxsx);
        }

        
        public static void showAllFiles(File dir) throws Exception {
            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());

                }
            }
        }
}

输出:

====================================================================================================

 

这个小例子 ,在这里添加随笔 是告诉自己, 能用程序处理的别耽误自己时间。

 

posted @ 2016-08-04 13:54  猪哥哥厉害  阅读(495)  评论(0编辑  收藏  举报