无法在Android中读取可序列化对象

时间:2016-03-09 16:54:53

标签: android file

我有这个类来读/写可序列化的对象:

public class SerializableManager {

/**
 * Saves a serializable object.
 *
 * @param context The application context.
 * @param objectToSave The object to save.
 * @param fileName The name of the file.
 * @param <T> The type of the object.
 */

public static <T extends Serializable> void saveSerializable(Context context, T objectToSave, String fileName) {
    try {
        FileOutputStream fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);

        objectOutputStream.writeObject(objectToSave);

        objectOutputStream.close();
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Loads a serializable object.
 *
 * @param context The application context.
 * @param fileName The filename.
 * @param <T> The object type.
 *
 * @return the serializable object.
 */

public static<T extends Serializable> T readSerializable(Context context, String fileName) {
    T objectToReturn = null;

    try {
        FileInputStream fileInputStream = context.openFileInput(fileName);
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        objectToReturn = (T) objectInputStream.readObject();

        objectInputStream.close();
        fileInputStream.close();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

    return objectToReturn;
}

/**
 * Removes a specified file.
 *
 * @param context The application context.
 * @param filename The name of the file.
 */

public static void removeSerializable(Context context, String filename) {
    context.deleteFile(filename);
}

}

问题在于,当我尝试从文件中读取对象myUserData时,尽管我努力将其写入文件,但它始终为null。 用于编写的代码:

SerializableManager.saveSerializable(this, myUserData, UserData.FILENAME);

用于阅读的代码:

UserData myUserData = SerializableManager.readSerializable(this, UserData.FILENAME);

文件名:

public static final String FILENAME = "OLA";

已经尝试访问创建文件的位置但我无法找到它。位置是: /data/user/0/com.example.user.cycle_to_shop_v1/files

老实说,Iam尝试了一切,但迄今为止没有任何工作。 感谢帮助, Rui Santos

1 个答案:

答案 0 :(得分:0)

嗯,这是我要序列化的类:

公共类UserData实现Serializable {

// Stores all data related to the user
private static final long serialVersionUID = -29238982928391L;

// Stores all user data in this file
public static final String FILENAME = "OLA";

// Singleton
private static UserData instance = null;

// Number of initial auto logins, which means, number of times the user can access the app without authentication
private static final int NUMBER_OF_AUTO_LOGINS = 2;

private String userName;
private String userEmail;
private Uri userPhotoUrl;
private int userAutoLogins;
private String userId;


protected  UserData(){}

public static UserData getUserDataInstance(){
    if(instance == null)
        instance = new UserData();

    return instance;
}

public String getUsername() {
    return userName;
}

public String getUserEmail() {
    return userEmail;
}

public Uri getUserPhotoUrl() {
    return userPhotoUrl;
}

public int getUserAutoLogins() {
    return userAutoLogins;
}

public String getUserId(){
    return userId;
}

public void setUsername(String newUsername) {
    userName = newUsername;
}

public void setUserEmail(String newUserEmail) {
    userEmail = newUserEmail;
}

public void setUserPhotoUrl(Uri newUserPhotoUrl) {
    userPhotoUrl = newUserPhotoUrl;
}

public void setUserAutoLogins(int newUserAutoLogins) {
    userAutoLogins = newUserAutoLogins;
}

public void setUserId(String newUserId){
    userId = newUserId;
}

}