packagecom.dataprocess;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.net.URL;importjava.net.URLConnection;importjava.util.List;importjava.util.Map;importjava.util.regex.Matcher;importjava.util.regex.Pattern;public classCopyOfGenerateGpx {public voidgenerate(){
String fileNames[]={"LocationData_2015-09-15","LocationData_2015-09-16"};int n =fileNames.length;for(String fileName: fileNames){//输入文件
File filein = new File("H:\\项目数据\\测试\\"+fileName+".txt");//输出文件
File fileout = new File("H:\\项目数据\\测试\\gpx\\"+fileName+".gpx");//输出json文件
File jsonfile = new File("H:\\项目数据\\测试\\json\\JSON_"+fileName+".json");try{//输入流
FileInputStream fis = newFileInputStream(filein);
InputStreamReader isr= newInputStreamReader(fis);
BufferedReader br= newBufferedReader(isr);//输出流
FileOutputStream fos = newFileOutputStream(fileout);
OutputStreamWriter osw= newOutputStreamWriter(fos);
BufferedWriter bw= newBufferedWriter(osw);//json输出流
FileOutputStream fos1 = newFileOutputStream(jsonfile);
OutputStreamWriter osw1= newOutputStreamWriter(fos1);
BufferedWriter bw1= newBufferedWriter(osw1);
bw1.write("[\r\n");
bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
bw.write("
+ " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\r\n");
bw.write("\r\n\r\n");
String line= null;
String regEx="[^0-9.\\+\\-\\s]";
Pattern p=Pattern.compile(regEx);while((line=br.readLine())!=null){
Matcher m=p.matcher(line);
String[] arrs=m.replaceAll("").trim().split("\\s");
String lonbd= arrs[2];
String latbd= arrs[3];
String params=
"coords="
+ lonbd+","+latbd+ "&from=1&to=5&ak=ZQU8ZGeBf95nN8wY1cNL13NP&output=json";
String s=sendGet("http://api.map.baidu.com/geoconv/v1/?", params);
String[] s1= s.split("\\[");
String[] s2= s1[1].split(",");
Matcher m1= p.matcher(s2[0]);
Matcher m2= p.matcher(s2[1]);
String[] arrs1=m1.replaceAll("").trim().split("\\s");
String[] arrs2=m2.replaceAll("").trim().split("\\s");
System.out.println(s1[1]);
System.out.println(arrs1[0]+" "+arrs2[0]);double lon = 2*Double.parseDouble(lonbd)-Double.parseDouble(arrs1[0]);double lat = 2*Double.parseDouble(latbd)-Double.parseDouble(arrs2[0]);
String str= "61.32015-09-05T14:12:20Z";
bw.write(str+"\r\n");
String str1= "{"
+"\"longitude\": "+lon+",\"latitude\": "+lat+"},\r\n";
bw1.write(str1);
System.out.println(str);
}
bw.write("\r\n\r\n
\r\n");bw.flush();
bw1.write("]\r\n");
bw1.flush();
br.close();
fis.close();
bw.close();
fos.close();
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}public staticString sendGet(String url, String param) {
String result= "";
BufferedReader in= null;try{
String urlNameString= url +param;
URL realUrl= newURL(urlNameString);//打开和URL之间的连接
URLConnection connection =realUrl.openConnection();//设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//建立实际的连接
connection.connect();//获取所有响应头字段
Map> map =connection.getHeaderFields();//遍历所有的响应头字段//for (String key : map.keySet()) {//System.out.println(key + "--->" + map.get(key));//}//定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(newInputStreamReader(
connection.getInputStream(),"utf-8"));
String line;while ((line = in.readLine()) != null) {
result+=line;
}
}catch(Exception e) {
System.out.println("发送GET请求出现异常!" +e);
e.printStackTrace();
}//使用finally块来关闭输入流
finally{try{if (in != null) {
in.close();
}
}catch(Exception e2) {
e2.printStackTrace();
}
}returnresult;
}public static voidmain(String[] args){
CopyOfGenerateGpx g= newCopyOfGenerateGpx();
g.generate();
}
}