java.io.NotSerializableException 与 ObjectOutputStream

java.io.NotSerializableException with ObjectOutputStream

我正在尝试将对象写入文件。结果returnjava.io.NotSerializableException,虽然我实现了Serializable

FileHandle.java:

public static void writeFile(File file, Object object) throws IOException {
    ObjectOutputStream output = null;
    FileOutputStream fileOutput = null;
    fileOutput = new FileOutputStream(file);
    output = new ObjectOutputStream(fileOutput);
    output.writeObject(object);
    if (output != null) {
        output.close();
    }
    if (fileOutput != null) {
        fileOutput.close();
    }
}

我要写的对象:Photo.java

public class Photo implements Serializable{
    private String backgroundUrl;
    private Color textBoxColor = Color.CORAL;
    private Message message = new Message();
    private Watermark watermark = new Watermark();

    public Photo(){
    }

    public Photo(String backgroundUrl, Color textBoxColor, Message message, Watermark watermark){
        this.backgroundUrl = backgroundUrl;
        this.textBoxColor = textBoxColor;
        this.watermark = watermark;
        this.message = message;
    }

    // getter and setter
}

Watermark.java:

public class Watermark implements Serializable{
    private String watermarkUrl;
    private int height = 85;
    private int width = 85;
    private Position position = Position.BOTTOM_RIGHT; // this is ENUM

    public Watermark(){
    }

    public Watermark(String watermarkUrl, Position position){
        this.watermarkUrl = watermarkUrl;
        this.position = position;
    }

    public Watermark(String watermarkUrl, int height, int width, Position position){
        this.watermarkUrl = watermarkUrl;
        this.height = height;
        this.width = width;
        this.position = position;
    }

    // getter and setter
}

Message.java:

public class Message implements Serializable{
    private String content;
    private Color color = Color.WHITE;
    private float size = 30f;

    // getter and setter
}

Position.java:

public enum Position {
    TOP_LEFT(1), TOP_RIGHT(2), BOTTOM_LEFT(3), BOTTOM_RIGHT(4);

    private int value;

    Position(int value){
        this.value = value;
    }

    public int getPosition(){
        return value;
    }
}

我猜问题出在枚举上,对吗?

感谢@user,我的代码成功了。问题出自javafx Color,我已经在model

中将其转换为awt Color
public class ColorUtil {

    public static Color fxToAwt(javafx.scene.paint.Color color){
        return new Color((float)color.getRed(), (float)color.getGreen(), (float)color.getBlue(), (float)color.getOpacity());
    }

    public static javafx.scene.paint.Color awtToFx(Color color){
        return new javafx.scene.paint.Color(color.getRed()/255.0, color.getGreen()/255.0, color.getBlue()/255.0, color.getAlpha()/255.0);
    }
}

您可以将 Color 值存储为字符串以进行序列化,并在以字符串形式检索值时将它们转换回 Colors。例如:

Color color = Color.White;  
String strColor = color.toString();

//now you can serialize strColor  
//when you want to retrieve it

String strColorRetrieved = .....//retrieved
Color colorRetrieved = Color.web(strColorRetrieved);