gpt4 book ai didi

Java 对象序列化 java.io.UTFDataFormatException

转载 作者:行者123 更新时间:2023-11-30 11:28:18 27 4
gpt4 key购买 nike

我有一个对象,我想序列化成字节码:

public class objektserial implements Serializable {

private static final long serialVersionUID = 1L;
private String msg ="";
private LinkedList<String> stringlist = new LinkedList<String>();

public objektserial(String nachricht) {
this.msg = nachricht;
this.stringlist = new LinkedList<String>();
}

public objektserial(){
}

public String getMsg(){
return msg;
}

public void setList(LinkedList<String> list){
this.stringlist = list;
}
}

我的想法:1.序列化2.加密字节码3.加密字节码4.反序列化

   // Serialization
objektserial os = new objektserial(textField.getText());
LinkedList<String> list = new LinkedList<String>();
for (int i = 0; i < 10; i++)
list.add("String Nr. " + i);
os.setList(list);

try {
FileOutputStream file = new FileOutputStream("objekt.objs");
ObjectOutputStream o = new ObjectOutputStream(file);
o.writeObject(os);
o.close();
} catch (IOException ex) {
System.err.println(e);
}

// Encrypt
try {
BufferedReader in = new BufferedReader(new FileReader("objekt.objs"));
String s = "";
String text = "";
while ((s = in.readLine()) != null) {
text += s;
}
String passwordEnc = AESencrp.encrypt(text);
String passwordDec = AESencrp.decrypt(passwordEnc);
File file = new File("objekt.objs");
FileWriter fw = new FileWriter(file, false);
BufferedWriter writer = new BufferedWriter(fw);

writer.write(passwordEnc);

writer.close();

System.out.println("Plain Text : " + text);
System.out.println("Encrypted Text : " + passwordEnc);
System.out.println("Decrypted Text : " + passwordDec);
} catch (Exception eiasdasd) {
}

// Decrypt
try {
BufferedReader in = new BufferedReader(new FileReader("objekt.objs"));
String s = "";
String text = "";
while ((s = in.readLine()) != null) {
text += s;
}
String passwordDec = AESencrp.decrypt(text);
File file = new File("objekt.objs");
FileWriter fw = new FileWriter(file, false);
BufferedWriter writer = new BufferedWriter(fw);

writer.write(passwordDec);

writer.close();

System.out.println("Plain Text : " + text);
System.out.println("Encrypted Text : " + text);
System.out.println("Decrypted Text : " + passwordDec);
} catch (Exception eiasdasd) {
}

// Read&Dezerialize
try {
FileInputStream file = new FileInputStream("objekt.objs");
ObjectInputStream o = new ObjectInputStream(file);
objektserial obs = (objektserial) o.readObject();
o.close();
textField_1.setText(obs.getMsg());
} catch (IOException e) {
System.err.println(e);
} catch (ClassNotFoundException e) {
System.err.println(e);
}

这很好用,但是如果我的序列化对象太大(超过一行,如果我用 Notepad++ 打开字节码文件),我会得到一个 "java.io.UTFDataFormatException"

当我尝试反序列化并再次读取解密的文件时,我得到了 java.io.UTFDataFormatException...

我该怎么做才能解决这个问题?

最佳答案

您正在使用 ObjectStream.writeObject() 编写。您需要使用 ObjectInputStream.readObject() 进行读取。

序列化对象不是线条。它们也不是字符数据,这排除了读者。

关于Java 对象序列化 java.io.UTFDataFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18927658/

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