我正在尝试保存/加载 GUI 的设置。
到目前为止,我已经使用属性进行了保存:
Properties prop = new Properties();
prop.setProperty("a", Boolean.valueOf(boxRandomMouse.isSelected()).toString());
prop.setProperty("b", listModelPath.toString());
我在这里遇到的问题是,我将 JList
的 listModelPath
保存到 String
中:
public static DefaultListModel<String> listModelPath;
在我的文件中,属性b
下。我找不到任何方法来加载此 listModelPath
并将其解析为我想要的类型,即 Position
。
类 Position
可以使用以下构造函数进行初始化:
Position newPos = new Position(int x, int y, int z);
或者对于数组:
public Position[] toTown = new Position[] {
new Position(3094, 3491, 0), new Position(3088, 3487, 0),
new Position(3080, 3475, 0)};
这是 listModelPath
的 String
保存到文件中时的样子
c=[[x\=3094, y\=3491, z\=0], [x\=3092, y\=3491, z\=0], [x\=3090, y\=3491, z\=0], [x\=3088, y\=3490, z\=0], [x\=3086, y\=3488, z\=0], [x\=3084, y\=3486, z\=0], [x\=3082, y\=3484, z\=0], [x\=3081, y\=3482, z\=0], [x\=3080, y\=3481, z\=0], [x\=3080, y\=3479, z\=0], [x\=3080, y\=3477, z\=0], [x\=2045, y\=5194, z\=0], [x\=2044, y\=5192, z\=0], [x\=2042, y\=5190, z\=0], [x\=2041, y\=5189, z\=0]]
我需要找到一种方法来将上面的字符串解析到我的位置中,或者以某种方式找到一种方法将该数组保存到文件中,老实说我不知道该怎么做。
我曾多次尝试以不同的方式做到这一点,但总是失败,所以这就是我来这里的原因。 希望你们知道如何处理我在这里遇到的情况。
PS。我刚刚开始阅读有关序列化的内容,也许这是要走的路?它是否允许我将在应用程序中动态创建的数组保存在文件中,而不将其转换为字符串并从字符串返回?最佳答案
您可以序列化您的 listModelPath
对象,然后将其作为对象发送到文件,然后您可以将其作为对象读取,然后将其转换回其真实的类
。
这可以通过将 ObjectOutputSteam
链接到底层流(例如 FileOutputStream
),然后将 Obejet 写入其中来完成(Position[]
)
public void writePositions() throws IOException, ClassNotFoundException {
File file = new File("positions.ser");
FileOutputStream outFile = new FileOutputStream(file);
ObjectOutputStream outStream = new ObjectOutputStream(outFile);
public Position[] toTown = new Position[] {
new Position(3094, 3491, 0),
new Position(3088, 3487, 0),
new Position(3080, 3475, 0)};
outStream.writeObject(toTown);
outStream.close();
outFile.close();
}
您可以从写入的文件中读取 Position
的序列化数组,positions.ser 如下所示:
public void readPositions() throws IOException, ClassNotFoundException {
File file = new File("positions.ser");
FileInputStream inFile = new FileInputStream(file);
ObjectInputStream inStream = new ObjectInputStream(inFile);
Position[] toTown = (Position[]) inStream.readObject());
for(int i = 0; i < toTown.length; i++) {
System.out.println(toTown[i]);
}
inStream.close();
inFile.close();
file.delete();
}
您应该注意,Position
类应该是Serialized
,否则 JVM 不会让您将这种类型的任何对象来回发送到内存文件中甚至通过任何流。
我不太确定我是否充分理解有关 listModelPath
的主要问题以及它与 Position
数组的关系,但您应该提供更多信息,尤其是您如何创建 listModelPath
实例。
编辑(保存许多不同类型的对象):
您不仅可以保存一个对象
,还可以保存任意数量的对象,无论它们是什么类型。将对象加载回 JVM 时,应按照写入的顺序读取它们,以避免出现 ClassCastException
:
public void writePositions() throws IOException, ClassNotFoundException {
...
public Position[] toTown = new Position[] {
new Position(3094, 3491, 0),
new Position(3088, 3487, 0),
new Position(3080, 3475, 0)};
outStream.writeObject(toTown); //Write the array of Position
boolean selected = boxRandomMouse.isSelected();
outStream.writeObject(selected); //Write the Boolean Object
...
}
public void readPositions() throws IOException, ClassNotFoundException {
...
Position[] toTown = (Position[]) inStream.readObject()); //Read the array of Position which was written first
for(int i = 0; i < toTown.length; i++) {
System.out.println(toTown[i]);
}
boolean selected = (boolean) inStream.readObject(); //Read the Boolean value which was written in the second order
// And so on
...
}
关于java - 将文件中的字符串追加到数组中并解析它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24228617/