我想从 Android 内部存储中读取对象。
以下是我的代码。我编写了一个静态函数,用于从同一类中的文件中读取对象。不知道为什么会发生这种异常。如果您能提出一些建议,真的很感激。
谢谢。
package com.crescent.programmercalculator;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import android.util.Log;
import android.content.Context;
public class CalculateConfigurations implements Serializable{
static String configLocation="configFile";
public short radix;
CalculateConfigurations(){
radix=16;
}
public static CalculateConfigurations loadObjectFromFile(Context context){
try {
FileInputStream fis = context.openFileInput(configLocation);
ObjectInputStream is = new ObjectInputStream(fis);
CalculateConfigurations config = (CalculateConfigurations) is.readObject();
is.close();
fis.close();
return config;
} catch (FileNotFoundException e) {
// first use case
Log.v("CalculateConfigurations", "first init for configuration file");
return new CalculateConfigurations();
} catch (IOException e) {
e.printStackTrace();
Log.e("CalculateConfigurations", "Fatal error, configuration file may be broken");
return new CalculateConfigurations();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
Log.e("CalculateConfigurations", "Fatal error, unknown");
return new CalculateConfigurations();
}
}
}