@huyl08
2016-08-27T15:07:17.000000Z
字数 6548
阅读 1528
Java
星迹迷航
文件读写
http://blog.jobbole.com/88993/
- 目前JDK版本为8u102,可前往Oracle Java官网下载最新版本的Java,可根据自身设备操作系统的不同选择相应的安装包版本。本版本的“新手上路”以JDK 8u102作为演示样例
- 基本的操作可学习官网新手入门(英语)、Codecademy(英语)、Java程序设计(中文)或其他类似资源。
- 完整的Java示例源码可通过该链接下载
- 推荐使用IntelliJ IDEA作为集成开发环境。
下载Windows下最新版本的Java安装包JDK 8u102,双击安装,根据自己需要设置安装位置。
安装成功后,若Java被顺利添加到Path环境变量中,运行命令行cmd.exe(可使用Windows键+R打开“运行”窗口,敲入“cmd”火车),键入以下命令即可看到Java版本信息
java -version
安装成功版本输出类似如下
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b15, mixed mode)
以下为Java源码样例
import java.util.ArrayList;
// 使用Coordinate类记录坐标位置
// 使用静态方法将输入的字符串转化为坐标数组
public class Coordinate{
// 经度坐标
public double x;
// 纬度坐标
public double y;
// Coorindate类的构造方法
public Coordinate(double x, double y){
this.x = x;
this.y = y;
}
// 静态方法
// 将输入的字符串转化为坐标数组
// 参数: geometry_string 记载几何形态的字符串
// 返回值: 记录坐标(Coordinate类)的数组(ArrayList)
public static ArrayList<Coordinate> geometry_parse(String geometry_string){
// 输入字符串为空或者长度为0,返回null值
if( geometry_string == null || geometry_string.length() == 0 )
return null;
// 声明返回值数组
ArrayList<Coordinate> results = new ArrayList<Coordinate>();
// 按“;”分隔字符串
String[] tokens = geometry_string.split(";");
for( String token : tokens ){
// 按":"拆分字符串获得经纬度坐标
String[] values = token.split(":");
double x = Double.parseDouble(values[0]);
double y = Double.parseDouble(values[1]);
results.add(new Coordinate(x, y));
}
return results;
}
}
import java.util.ArrayList;
// 使用Coordinate类记录坐标位置
// 使用静态方法将输入的字符串转化为坐标数组
public class Coordinate{
// 经度坐标
public double x;
// 纬度坐标
public double y;
// Coorindate类的构造方法
public Coordinate(double x, double y){
this.x = x;
this.y = y;
}
// 静态方法
// 将输入的字符串转化为坐标数组
// 参数: geometry_string 记载几何形态的字符串
// 返回值: 记录坐标(Coordinate类)的数组(ArrayList)
public static ArrayList<Coordinate> geometry_parse(String geometry_string){
// 输入字符串为空或者长度为0,返回null值
if( geometry_string == null || geometry_string.length() == 0 )
return null;
// 声明返回值数组
ArrayList<Coordinate> results = new ArrayList<Coordinate>();
// 按“;”分隔字符串
String[] tokens = geometry_string.split(";")
for( String token : tokens ){
String[] values = token.split(":")
double x = Double.parseDouble(values[0]);
double y = Double.parseDouble(values[1]);
results.add(new Coordinate(x, y));
}
return results;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
public class LinkReader{
// 定义静态类Link,用于存储路段信息
public static class Link{
public int link_id;
public int from_node;
public int to_node;
public double link_length;
public int link_class;
// 坐标序列
public ArrayList<Coordinate> coordinates;
// 将坐标变量转化为字符串
public String coordinateToString(){
if( coordinates == null || coordinates.size() == 0 )
return "";
String geomStr = "";
for( Coordinate coordinate : coordinates ){
geomStr += String.format("%.6f:%.6f\t", coordinate.x, coordinate.y);
}
return geomStr;
}
}
// 静态读文件函数
// 参数: file_path为输入文件路径
// 返回值: 返回路段属性字典(HashMap)
public static HashMap<Integer, Link> readLink(String file_path){
try{
// 生成返回的HashMap字典对象
HashMap<Integer, Link> results = new HashMap<Integer, Link>();
// 创建文件输入流,使用BufferedReader按行读取
BufferedReader reader = new BufferedReader(new FileReader(new File(file_path)));
// 声明存储文件行的临时变量
String line = null;
// 读入行到临时变量line中, 在不为空的情况下将其转化为Link对象并存储
while( ( line = reader.readLine() ) != null ){
StringTokenizer tokenizer = new StringTokenizer(line.trim(), ",");
Link link = new Link();
link.link_id = Integer.parseInt(tokenizer.nextToken());
link.from_node = Integer.parseInt(tokenizer.nextToken());
link.to_node = Integer.parseInt(tokenizer.nextToken());
link.link_length = Double.parseDouble(tokenizer.nextToken());
link.link_class = Integer.parseInt(tokenizer.nextToken());
link.coordinates = Coordinate.geometry_parse(tokenizer.nextToken());
results.put(link.link_id, link);
}
// 关闭文件流
reader.close();
// 返回结果
return results;
}catch(IOException e){
e.printStackTrace();
return null;
}
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GPS {
// 车辆编号
public String vid;
// 记录时间戳
public Date datetime;
// 经度
public double x;
// 纬度
public double y;
// 速度,单位为km/h
public double speed;
// 瞬时方向角,单位为度,正北为0,顺时针为正方向
public double direction;
// 日期时间(Date)和字符串的转化辅助类
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
public static SimpleDateFormat sdf_str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 按读入的格式化字符串生成GPS实例
public GPS(String line) throws ParseException {
String[] tokens = line.trim().split(",");
this.vid = new String(tokens[0]);
this.datetime = sdf.parse(tokens[1]);
this.x = Double.parseDouble(tokens[2]);
this.y = Double.parseDouble(tokens[3]);
this.speed = Double.parseDouble(tokens[4]);
this.direction = Double.parseDouble(tokens[5]);
}
public String toString(){
return String.format("%s\t%s\t(%.6f, %.6f)", this.vid, sdf_str.format(this.datetime), this.x, this.y);
}
// 求和另一条记录之间的时间差, 以秒为单位
public double timeDifference(GPS another){
return ((double)( another.datetime.getTime() - this.datetime.getTime() ) / 1000.0 );
}
// 求和另一条记录之间的距离
// 注意:实际计算中需要做经纬度到以米为单位的投影转换
public double distance(GPS another){
double dx = another.x - this.x;
double dy = another.y - this.y;
return Math.sqrt( dx * dx + dy * dy );
}
}
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
public class StarTrek {
public static void main(String[] args) throws ParseException {
GPS gps_a = new GPS("13381083463,20160701120147,116.387472,39.889589,26.00,266");
GPS gps_b = new GPS("13311492120,20160701120144,116.355589,39.993436,10.00,260");
System.out.println(String.format("GPS A is %s", gps_a.toString()));
System.out.println(String.format("GPS B is %s", gps_b.toString()));
System.out.println(String.format("Disatance between A and B is %.5f degrees", gps_a.distance(gps_b)));
System.out.println(String.format("Time difference between A and B is %.0f seconds", gps_a.timeDifference(gps_b)));
HashMap<Integer, LinkReader.Link> links = LinkReader.readLink("gis.txt");
System.out.println("\nLinkID\tLinkClass\tFromNode\tToNode\tLinkLength\tGeometry");
Iterator<Integer> iterator = links.keySet().iterator();
while( iterator.hasNext() ){
Integer linkid = iterator.next();
LinkReader.Link link = links.get(linkid);
System.out.println(String.format("%d\t%d\t%d\t%d\t%.1f\t%s", linkid, link.link_class,
link.from_node, link.to_node, link.link_length, link.coordinateToString()));
}
}
}
1,3,4,280.3,2,115.488988:39.917917;115.488997:39.917938;115.489045:39.918061;115.489219:39.918316;115.489335:39.918486;115.489366:39.918540;115.489382:39.918612;115.489385:39.918672;115.489373:39.918713
3,7,8,173.2,2,115.381547:39.950776;115.381786:39.950461;115.381848:39.950355;115.381887:39.950260;115.381913:39.950163
57952495354,20160701120100,116.372966,39.908912,26.00,262
13381083463,20160701120147,116.387472,39.889589,26.00,266
13311492120,20160701120144,116.355589,39.993436,10.00,260
13439610594,20160701120147,116.394988,39.940625,0.00,0