我在项目中声明了一个自定义类:
public class LocationData {
private Location location;
private LocalDateTime localDateTime;
private int heartRate;
private int calories;
private int ropeMoves;
private int dumbbellMoves;
private int pedalRotations;
private int wheelRotations;
private int numberOfSteps;
public LocationData(Location location, LocalDateTime localDateTime, int heartRate, int calories, int ropeMoves, int dumbbellMoves, int pedalRotation, int wheelRotations, int numberOfSteps) {
this.location = location;
this.localDateTime = localDateTime;
this.heartRate = heartRate;
this.calories = calories;
this.ropeMoves = ropeMoves;
this.dumbbellMoves = dumbbellMoves;
this.pedalRotations = pedalRotations;
this.wheelRotations = wheelRotations;
this.numberOfSteps = numberOfSteps;
}
public Location getLocation() {
return location;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
// Other getters
@Override
public String toString() {
return "LocationData[" +
"ntlocation=" + location +
"ntlocalDateTime=" + localDateTime +
"ntcalories=" + calories +
"ntheartRate=" + heartRate +
"ntropeMoves=" + ropeMoves +
"ntdumbbellMoves=" + dumbbellMoves +
"ntpedalRotations=" + pedalRotations +
"ntwheelRotations=" + wheelRotations +
"ntnumberOfSteps=" + numberOfSteps +
"]";
}
}
它表示一个位置加上一些信息。然后我保存List
LocationData
来创建"路由"。
我需要将此列表(称为location
)保存到文件中,因为将来用户会要求检索它以创建GPX文件。
我认为最好的解决方案是使LocationData
类可序列化,但我不知道如何序列化(然后反序列化)它。 所以...我需要了解如何:
- 序列化
LocationData
类 - 反序列化
LocationData
类 - 创建序列化
LocationData
列表 - 将列表写入文件
- 从文件中读取列表
implements Serializable
添加到类定义中,但不必实现 Serializable
的方法 - Object 已经有一个默认实现,并且 Serializable 接口用作声明性接口。
您还需要确保Location
和LocalDateTime
都是可序列化的。
最后,一旦全部就绪,您就可以使用 ObjectInputStream/ObjectOutputStream 来读取和写入对象。