Java 使用ObjectOutputStream保存HashMap
为了在命令中保存远程传送点,我有一个Java 使用ObjectOutputStream保存HashMap,java,serialization,objectoutputstream,bukkit,Java,Serialization,Objectoutputstream,Bukkit,为了在命令中保存远程传送点,我有一个哈希映射: public HashMap<Player, Location> mapHomes = new HashMap<>(); 由于重新启动时应保持这些设置,因此我实现了一种保存方法: public void save(HashMap<Player,Location> mapHome, String path) throws NotSerializableException{ try{ Obj
哈希映射
:
public HashMap<Player, Location> mapHomes = new HashMap<>();
由于重新启动时应保持这些设置,因此我实现了一种保存方法:
public void save(HashMap<Player,Location> mapHome, String path) throws NotSerializableException{
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeObject(mapHome);
oos.flush();
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void save(HashMap mapHome,字符串路径)引发NotSerializableException{
试一试{
ObjectOutputStream oos=新的ObjectOutputStream(新文件输出流(路径));
oos.writeObject(mapHome);
oos.flush();
oos.close();
}捕获(例外e){
e、 printStackTrace();
}
}
但它不起作用。它抛出NotSerializableException
我认为主要的问题是Player
和Location
不是可序列化的类型,那么我应该如何编写这个HashMap
class Player implements Serializable {}
class Location implements Serializable {}
请记住,您只能序列化实现Serializable
接口的对象。
因此,您的
Player
和Location
类也必须实现该接口。我认为您正在将HashMap
写入文件
您需要做的是使您的播放器
和位置
类可序列化
public class Player implements java.io.Serializable {
// ...
}
public class Location implements java.io.Serializable {
// ...
}
已可序列化。
HashMap
已可序列化
public class Player implements java.io.Serializable {
// ...
}
public class Location implements java.io.Serializable {
// ...
}
问题是映射中的对象不是,因此您也必须使它们可序列化
public class SerializedPlayer extends Player implements Serializable {
public SerializedPlayer() {}
public SerializedPlayer(Player playerToClone) {
this.setField1(playerToClone.getField1());
// Set all the fields
}
}
添加到地图时:
map.put(new SerializedPlayer(player), new SerializedLocation(location));
当实例需要具有Serializable
接口时,将引发notserializableeexception
class YourClass implements Serializable {
// ...
}
要使Location
可序列化,我建议使用。
我自己写的,只是要求你在代码中给我信用
package com.github.JamesNorris.Class;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
/**
* The class that allows location to be serialized, and be used
* in a file. This class should not be changed, otherwise it
* will not work for older files.
*
* @author Jnorr44
*/
public final class SerializableLocation implements Serializable {
private static final long serialVersionUID = 8650311534439769069L;
private final String world;
private final String uuid;
private final double x, y, z;
private final float yaw, pitch;
private transient Location loc;
/**
* Creates a new SerializableLocation instance of any org.bukkit.Location.
*
* @param l
*/
public SerializableLocation(Location l) {
this.world = l.getWorld().getName();
this.uuid = l.getWorld().getUID().toString();
this.x = l.getX();
this.y = l.getY();
this.z = l.getZ();
this.yaw = l.getYaw();
this.pitch = l.getPitch();
}
/**
* Gets the org.bukkit.Location back from any SerializableLocation.
*
* @param l
* @return
*/
public static Location returnLocation(SerializableLocation l) {
float pitch = l.pitch;
float yaw = l.yaw;
double x = l.x;
double y = l.y;
double z = l.z;
World world = Bukkit.getWorld(l.world);
Location location = new Location(world, x, y, z, yaw, pitch);
return location;
}
// FROM HERE ON NEEDS DOC NOTES
public SerializableLocation(Map<String, Object> map) {
this.world = (String) map.get("world");
this.uuid = (String) map.get("uuid");
this.x = (Double) map.get("x");
this.y = (Double) map.get("y");
this.z = (Double) map.get("z");
this.yaw = ((Float) map.get("yaw")).floatValue();
this.pitch = ((Float) map.get("pitch")).floatValue();
}
public final Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("world", this.world);
map.put("uuid", this.uuid);
map.put("x", this.x);
map.put("y", this.y);
map.put("z", this.z);
map.put("yaw", this.yaw);
map.put("pitch", this.pitch);
return map;
}
public final Location getLocation(Server server) {
if (loc == null) {
World world = server.getWorld(this.uuid);
if (world == null) {
world = server.getWorld(this.world);
}
loc = new Location(world, x, y, z, yaw, pitch);
}
return loc;
}
}
之所以需要这样做,是因为Location
包含world
、x
、y
、z
、yaw
和pitch
,其中world
是不可序列化的。事实上,您正在尝试序列化Player
和Location
,尝试实现新类等,但为什么要这样做
public class Player implements java.io.Serializable {
// ...
}
public class Location implements java.io.Serializable {
// ...
}
最好不要存储Player
和Location
对象,而是存储它们的字符串表示形式。例如,您可以使用Player.getName()
和类似“world:100:65:100”
的内容作为位置(这样我们就可以通过String.split(“:”
)轻松获取所有数据。我认为这是一种更好的方法