2016-07-28 78 views
-1

嗨我有一个简单的序列化问题错误/非功能序列化

这是我的全班同学。无干将,setter和尝试&抓

public class myTrip implements Serializable { 
String NazovTripu; 
int den, mesiac, rok; 
String Mesto; 
String filename="prve.dat"; 

public String getFilename() { 
    return filename; 
} 

public void Serializuj(Context context){ 
    FileOutputStream fos = null; 
    fos = context.openFileOutput(filename, Context.MODE_PRIVATE); 
    ObjectOutputStream os = new ObjectOutputStream(fos); 
    os.writeObject(this); 
    os.close();  
} 

public myTrip DeSerializuj(Context context) { 
    FileInputStream fis = null;  
     fis = context.openFileInput(filename); 

    ObjectInputStream is = new ObjectInputStream(fis); 
    myTrip mojtrip = (myTrip) is.readObject(); 
    is.close(); 
    fis.close(); 
     return mojtrip;   
    return null; 
} 

public void DeSerializuj2(Context context) { 
    FileInputStream fis = null; 

     fis = context.openFileInput(filename); 

    ObjectInputStream is = new ObjectInputStream(fis); 
    myTrip simpleClass = (myTrip) is.readObject(); 
    is.close(); 
    fis.close(); 

} 

我这里有两个变种反序列化,但没有工作,我不知道哪里有问题。如果序列化或反序列化,请帮我:)

我在一个活动

myTrip prvy= new myTrip(); 
    ... 
    prvy.Serializuj(this); 

用这个和这个代码在onCreate方法等活动

myTrip prvy= new myTrip(); 
... 
prvy.DeSerializuj(this); 

应用程序不会崩溃,但varianles没有价值。

+0

此代码不能编译。发布真实的代码。摆脱'DeSerializuj2()'。它没有任何用处。它应该返回反序列化的对象,在这种情况下它将与'DeSerializuj()'相同。 – EJP

回答

0

这里有很好的序列化和反序列化代码..

public void Serializuj(myTrip serTrip){ 
    FileOutputStream fos = null; 
    try { 
     File file = new File(getApplicationContext().getFilesDir(), "data.dat"); 
     FileOutputStream fo = new FileOutputStream(file); 
     ObjectOutputStream ou = new ObjectOutputStream(fo); 
     ou.writeObject(serTrip); 
     ou.close(); 
     fo.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public myTrip DeSerializuj() { 
    myTrip prvy = null; 
    try { 
     File file = new File(getApplication().getFilesDir(), "data.dat"); 
     FileInputStream fi = new FileInputStream(file); 
     ObjectInputStream oi = new ObjectInputStream(fi); 
    prvy = (myTrip) oi.readObject(); 
     oi.close(); 
     fi.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return prvy; 
}