java读文件,可能会用hashmap和arraylist
发布网友
发布时间:2022-05-18 18:58
我来回答
共5个回答
热心网友
时间:2023-11-15 18:54
帮你都读出来 我相信处理数据你能自己完成
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author Jeky
*/
public class FileDemo {
private static List<Location> createLocations(String filename) {
List<Location> locations = new LinkedList<Location>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();
Location currentLocation = null;
while (line != null) {
if (line.startsWith("Location:")) {
String[] split = line.split("\\s+");
float longitude = Float.parseFloat(split[1].trim());
float latitude = Float.parseFloat(split[2].trim());
currentLocation = new Location(longitude, latitude);
locations.add(currentLocation);
} else {
int lastIndex = line.lastIndexOf(" ");
String dateString = line.substring(0, lastIndex);
String valueString = line.substring(lastIndex + 1);
Date date = FORMAT.parse(dateString.trim());
float value = Float.parseFloat(valueString.trim());
currentLocation.getDatas().add(new MeasureData(date, value));
}
line = reader.readLine();
}
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
return locations;
}
public static void main(String[] args) {
List<Location> locations = createLocations("1.txt");
for (Location l : locations) {
System.out.println(l.getLongitude() + "\t" + l.getLatitude());
for (MeasureData d : l.getDatas()) {
System.out.println(d.getDate() + "\t" + d.getValue());
}
}
}
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
}
class Location {
public Location(float longitude, float latitude) {
this.longitude = longitude;
this.latitude = latitude;
this.datas = new LinkedList<MeasureData>();
}
public List<MeasureData> getDatas() {
return datas;
}
public float getLatitude() {
return latitude;
}
public float getLongitude() {
return longitude;
}
private float longitude;
private float latitude;
private List<MeasureData> datas;
}
class MeasureData {
public MeasureData(Date date, float value) {
this.date = date;
this.value = value;
}
public float getValue() {
return value;
}
public Date getDate() {
return date;
}
private Date date;
private float value;
}
热心网友
时间:2023-11-15 18:54
可以用java中的文件流实现,伪代码:
List l<String> = new ArrayList<String>();
String str = "";
while(){//循环读取
if(){//以Location开头的行
str = "insert inot XXX values(经度,纬度"
continue;
}
str=str+时间,值);
l.add(str);
}
最后遍历l把语句执行一遍插入数据库,然后统计
热心网友
时间:2023-11-15 18:55
--Split;
这种东西都是用 Perl/Ruby 做--JAVA写不够累4的~~
热心网友
时间:2023-11-15 18:55
读一行,判断一location开头就是一个位置,到下一个location之前是一组数据
热心网友
时间:2023-11-15 18:56
你想要什么?
让人给你代码?还是说说思路?追问大致的思路。有代码当然更好了。主要的问题是怎么才能把每个位置分开呢。