2013-09-21 100 views
2

我有一個對象,我想序列成字節碼:Java對象序列化java.io.UTFDataFormatException

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); 
} 

這工作正常,但如果我的序列化對象過大(超過一行,如果我用記事本打開++中的字節碼文件),我得到一個"java.io.UTFDataFormatException"

我得到java.io.UTFDataFormatException當我嘗試反序列化&閱讀再次解密的文件...

我能做些什麼來解決這個問題?

+1

上面的代碼有很多錯誤。您正在讀取和寫入同一個文件,您不需要中間文件。您將二進制(字節數組)視爲字符串值,反之亦然。我沒有看到任何解密方法。修復縮進並遵守Java風格指南也會有很多幫助。我的建議是重新開始並創建單獨的部分來完成文件寫入,編碼/解碼,字符編碼解碼和加密/解密*並單獨測試*。 –

回答

1

您正在使用ObjectStream.writeObject()編寫代碼。你需要用ObjectInputStream.readObject()來讀取。

序列化對象不是行。它們也不是字符數據,它排除了讀者。

+0

我正在閱讀ObjectInputstream(請參見上一個Methode)。 我正在閱讀僅用於加密和解密的行。 但問題就解決了: - 只是不得不在加密和解密時+ 「\ n」 而((S = in.readLine())!= NULL){ 文字+ = S +「\ n添加「; } 現在它工作完美,即使是更大的物體! :) –

+0

您正在閱讀和引用BufferedReader in = new BufferedReader(new FileReader(「objekt.objs」);'。由於我已經給出的原因,這是無效的。您需要讀取並加密*字節。* – EJP

+0

@EJP讀取和*解密*字節:P –