gpt4 book ai didi

java IO不可序列化异常

转载 作者:行者123 更新时间:2023-12-02 01:05:13 24 4
gpt4 key购买 nike

我使用 javafx fxml 文件作为我的 View 我正在尝试通过激活 Controller 中的方法并通过文件选择器传递 url 并传递我的文本字段之一作为文件选择器所需的窗口,将某些对象序列化到文件中:

Controller类中的方法

public void saveAsFile(ActionEvent actionEvent)
{
Window stage = searchByRatioTextFeild.getScene().getWindow();
DataBase.saveToFile(stage);
}

DataBase类中的方法:

 private static String fileLocation;
private static File currentFile;

public static void saveToFile(Window stage)
{
File file;
FileChooser fc = new FileChooser();
fc.setTitle("Save");
fc.setInitialFileName("all data");
file = fc.showSaveDialog(stage);
currentFile = file;
try {
saveItems();
} catch (IOException e) { }
}

public static void saveItems() throws IOException
{

//seriallise
FileOutputStream fo = new FileOutputStream(currentFile);
ObjectOutputStream output = new ObjectOutputStream(fo);
for (Item item : ItemList.items)
{
output.writeObject(item);
}
output.close();
fo.close();
}

我的元素列表包括:

public static ArrayList<Item> items;

我的元素是:

private String ratio;
private String classs;
private String voltAmper;
private String voltageLevel;
private String ITH;

public Item (String ratio,String classs,String voltAmper,String voltageLevel,String ITH)
{
this.ratio = ratio;
this.classs = classs;
this.voltAmper = voltAmper;
this.voltageLevel = voltageLevel;
this.ITH = ITH;
}

public String getRatio() {
return ratio;
}

public void setRatio(String ratio) {
this.ratio = ratio;
}

public String getClasss() {
return classs;
}

public void setClasss(String classs) {
this.classs = classs;
}

public String getVoltAmper() {
return voltAmper;
}

public void setVoltAmper(String voltAmper) {
this.voltAmper = voltAmper;
}

public String getVoltageLevel() {
return voltageLevel;
}

public void setVoltageLevel(String voltageLevel) {
this.voltageLevel = voltageLevel;
}

public String getITH() {
return ITH;
}

public void setITH(String ITH) {
this.ITH = ITH;
}

我没有收到错误消息,但文件不会保存为序列化数组列表,而是将其存储在文件中:

¬í {sr  java.io.NotSerializableException(Vx ç†5  xr java.io.ObjectStreamExceptiondÃäk9ûß  xr java.io.IOExceptionl€sde%ð«  xr java.lang.ExceptionÐý>;Ä  xr java.lang.ThrowableÕÆ5'9w¸Ë L causet Ljava/lang/Throwable;L 
detailMessaget Ljava/lang/String;[
stackTracet [Ljava/lang/StackTraceElement;L suppressedExceptionst Ljava/util/List;xpq ~ t
model.Itemur [Ljava.lang.StackTraceElement;F*<<ý"9 xp :sr java.lang.StackTraceElementa Åš&6Ý… I
lineNumberL declaringClassq ~ L fileNameq ~ L
methodNameq ~ xp  t java.io.ObjectOutputStreamt ObjectOutputStream.javat writeObject0sq ~
\q ~ q ~ t writeObjectsq ~
)t model.DataBaset
DataBase.javat saveItemssq ~
q ~ q ~ t
saveToFilesq ~
St controller.Controllert Controller.javat
saveAsFilesq ~
ÿÿÿþt $sun.reflect.NativeMethodAccessorImplt NativeMethodAccessorImpl.javat invoke0sq ~
>q ~ q ~ t invokesq ~
+t (sun.reflect.DelegatingMethodAccessorImplt !DelegatingMethodAccessorImpl.javaq ~ #sq ~
òt java.lang.reflect.Methodt Method.javaq ~ #sq ~
Gt sun.reflect.misc.Trampolinet MethodUtil.javaq ~ #sq ~
ÿÿÿÿt $sun.reflect.GeneratedMethodAccessor1pq ~ #sq ~
+q ~ %q ~ &q ~ #sq ~
òq ~ (q ~ )q ~ #sq ~
t sun.reflect.misc.MethodUtilq ~ ,q ~ #sq ~
ét $javafx.fxml.FXMLLoader$MethodHandlert FXMLLoader.javaq ~ #sq ~
yt 3javafx.fxml.FXMLLoader$ControllerMethodEventHandlerq ~ 5t handlesq ~
Vt *com.sun.javafx.event.CompositeEventHandlert CompositeEventHandler.javat dispatchBubblingEventsq ~
ît (com.sun.javafx.event.EventHandlerManagert EventHandlerManager.javaq ~ <sq ~
¿q ~ >q ~ ?q ~ <sq ~
:t )com.sun.javafx.event.BasicEventDispatchert BasicEventDispatcher.javat
dispatchEventsq ~
rt +com.sun.javafx.event.EventDispatchChainImplt EventDispatchChainImpl.javaq ~ Dsq ~
Jt com.sun.javafx.event.EventUtilt EventUtil.javat
fireEventImplsq ~
1q ~ Iq ~ Jt fireEventsq ~
Æt javafx.event.Eventt
Event.javaq ~ Msq ~
Ît javafx.scene.control.MenuItemt
MenuItem.javat firesq ~
}t Fcom.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainert ContextMenuContent.javat doSelectsq ~
Nq ~ Vq ~ Wt lambda$createChildren$343sq ~
Út Ccom.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecordq ~ ;t handleBubblingEventsq ~
Pq ~ :q ~ ;q ~ <sq ~
îq ~ >q ~ ?q ~ <sq ~
¿q ~ >q ~ ?q ~ <sq ~
;t -com.sun.javafx.event.CompositeEventDispatchert CompositeEventDispatcher.javaq ~ <sq ~
:q ~ Bq ~ Cq ~ Dsq ~
rq ~ Fq ~ Gq ~ Dsq ~
8q ~ Bq ~ Cq ~ Dsq ~
rq ~ Fq ~ Gq ~ Dsq ~
8q ~ Bq ~ Cq ~ Dsq ~
rq ~ Fq ~ Gq ~ Dsq ~
8q ~ Bq ~ Cq ~ Dsq ~
rq ~ Fq ~ Gq ~ Dsq ~
Jq ~ Iq ~ Jq ~ Ksq ~
6q ~ Iq ~ Jq ~ Msq ~
Æq ~ Oq ~ Pq ~ Msq ~
­t javafx.scene.Scene$MouseHandlert
Scene.javat processsq ~

q ~ pq ~ qt access$1500sq ~
ât javafx.scene.Sceneq ~ qt impl_processMouseEventsq ~
¾t $javafx.scene.Scene$ScenePeerListenerq ~ qt
mouseEventsq ~
Št Fcom.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificationt GlassViewEventHandler.javat runsq ~
'q ~ |q ~ }q ~ ~sq ~
ÿÿÿþt java.security.AccessControllert AccessController.javat doPrivilegedsq ~
°t /com.sun.javafx.tk.quantum.GlassViewEventHandlerq ~ }t lambda$handleMouseEvent$358sq ~
…t (com.sun.javafx.tk.quantum.QuantumToolkitt QuantumToolkit.javat runWithoutRenderLocksq ~
¯q ~ …q ~ }t handleMouseEventsq ~
+t com.sun.glass.ui.Viewt View.javaq ~ Œsq ~
©q ~ Žq ~ t notifyMousesq ~
ÿÿÿþt #com.sun.glass.ui.win.WinApplicationt WinApplication.javat _runLoopsq ~
±q ~ “q ~ ”t lambda$null$152sq ~
ìt java.lang.Threadt Thread.javaq ~ ~sr &java.util.Collections$UnmodifiableListü%1µìŽ L listq ~ xr ,java.util.Collections$UnmodifiableCollectionB €Ë^÷ L ct Ljava/util/Collection;xpsr java.util.ArrayListxÒ™Ça I sizexp w xq ~  x

我的代码出了什么问题,如何制作序列化文件以便稍后加载?

最佳答案

i get no error messages...

那是因为你忽略了异常..

} catch (IOException e) { }

其次,你不能像这样存储对象,你必须首先将该对象转换为字符串或 JSON 字符串,然后将其保存为文件。

注意:如果您想存储在数据库中,那么最好对其进行编码

关于java IO不可序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com