JavaFX:在二进制文件中保存带有图像 属性 的对象
JavaFX: Save an object with an Image property in a binary file
我正在尝试将 class 和图像 (javaFX) 属性 保存在二进制文件中,但是当图像不同于 null 时,我得到一个 IOException。
是否可以通过使用 Image 对象或其他方式来完成这项工作?
这是一个显示问题的演示 class。
import java.io.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.image.Image;
public class Demo extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
TheObject obj1 = new TheObject(1, new Image("image1.jpg"));
TheObject obj2 = new TheObject(2, new Image("image2.jpg"));
TheObject obj3 = new TheObject(3, new Image("image3.jpg"));
ObjectOutputStream outputStream;
try {
outputStream = new ObjectOutputStream(new FileOutputStream("file"));
outputStream.writeObject(obj1);
outputStream.writeObject(obj2);
outputStream.writeObject(obj3);
}
catch(IOException e) {
System.out.println("IOException");
System.exit(0);
}
}
public static class TheObject {
private int id;
private Image img;
TheObject(int id, Image img) {
this.id = id;
this.img = img;
}
public int getID() { return id; }
public Image getImg() { return img; }
public void setID(int id) { this.id = id; }
public void setImg(Image img) { this.img = img; }
}
}
首先,看看 this 我不知道你为什么不 打印你的堆栈跟踪。
为了帮助您,您需要
public static class TheObject implements Serializable
此外,将此添加到对象中:
TheObject() { //default constructor
}
最后,如果 import javafx.scene.image.Image 没有实现可序列化接口,你必须做类似 this
的事情
不过我真的有问题(5 分钟前发布)。你可能也会面对它......看看:
编辑: 如果你打印了你的堆栈跟踪,google 会告诉你通往这个的路...
Image
不可序列化,因此您需要实现自定义(反)序列化。您可以将图像转换为 BufferedImage
并使用 ImageIO
将数据写入 ObjectOutputStream
以执行此操作。
另外TheObject
需要实施Serializable
:
public static class TheObject implements Serializable {
private int id;
private transient Image img; // not written to stream via default serialisation
TheObject(int id, Image img) {
this.id = id;
this.img = img;
}
...
// custom serialisation
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject(); // write non-transient data
// write image if not null
out.writeBoolean(img != null);
if (img != null) {
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", out);
}
}
// custom deserialisation
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
// read non-transient data
in.defaultReadObject();
// read image if not null
if (in.readBoolean()) {
img = new Image(in);
} else {
img = null;
}
}
}
还记得关闭输出流或更好:使用try-with-resources:
try (FileOutputStream fos = new FileOutputStream("file");
ObjectOutputStream outputStream = new ObjectOutputStream()) {
outputStream.writeObject(obj1);
outputStream.writeObject(obj2);
outputStream.writeObject(obj3);
} catch(IOException e) {
System.out.println("IOException");
System.exit(0);
}
我正在尝试将 class 和图像 (javaFX) 属性 保存在二进制文件中,但是当图像不同于 null 时,我得到一个 IOException。
是否可以通过使用 Image 对象或其他方式来完成这项工作?
这是一个显示问题的演示 class。
import java.io.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.image.Image;
public class Demo extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
TheObject obj1 = new TheObject(1, new Image("image1.jpg"));
TheObject obj2 = new TheObject(2, new Image("image2.jpg"));
TheObject obj3 = new TheObject(3, new Image("image3.jpg"));
ObjectOutputStream outputStream;
try {
outputStream = new ObjectOutputStream(new FileOutputStream("file"));
outputStream.writeObject(obj1);
outputStream.writeObject(obj2);
outputStream.writeObject(obj3);
}
catch(IOException e) {
System.out.println("IOException");
System.exit(0);
}
}
public static class TheObject {
private int id;
private Image img;
TheObject(int id, Image img) {
this.id = id;
this.img = img;
}
public int getID() { return id; }
public Image getImg() { return img; }
public void setID(int id) { this.id = id; }
public void setImg(Image img) { this.img = img; }
}
}
首先,看看 this 我不知道你为什么不 打印你的堆栈跟踪。
为了帮助您,您需要
public static class TheObject implements Serializable
此外,将此添加到对象中:
TheObject() { //default constructor
}
最后,如果 import javafx.scene.image.Image 没有实现可序列化接口,你必须做类似 this
的事情不过我真的有问题(5 分钟前发布)。你可能也会面对它......看看:
编辑: 如果你打印了你的堆栈跟踪,google 会告诉你通往这个的路...
Image
不可序列化,因此您需要实现自定义(反)序列化。您可以将图像转换为 BufferedImage
并使用 ImageIO
将数据写入 ObjectOutputStream
以执行此操作。
另外TheObject
需要实施Serializable
:
public static class TheObject implements Serializable {
private int id;
private transient Image img; // not written to stream via default serialisation
TheObject(int id, Image img) {
this.id = id;
this.img = img;
}
...
// custom serialisation
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject(); // write non-transient data
// write image if not null
out.writeBoolean(img != null);
if (img != null) {
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", out);
}
}
// custom deserialisation
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
// read non-transient data
in.defaultReadObject();
// read image if not null
if (in.readBoolean()) {
img = new Image(in);
} else {
img = null;
}
}
}
还记得关闭输出流或更好:使用try-with-resources:
try (FileOutputStream fos = new FileOutputStream("file");
ObjectOutputStream outputStream = new ObjectOutputStream()) {
outputStream.writeObject(obj1);
outputStream.writeObject(obj2);
outputStream.writeObject(obj3);
} catch(IOException e) {
System.out.println("IOException");
System.exit(0);
}