我正在嘗試做某種序列化,我可以直接從文件讀取和寫入對象。Java Android中的EOFException?需要幫助
我剛開始寫一個字符到文件並試圖讀取它。這總是給我EOF
例外。
我正在Android設備上嘗試它。這裏是我的代碼:
public class TestAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
WriteToFile();
Load();
} catch (Exception e) {
e.printStackTrace();
}
}
public void Load() throws IOException
{
InputStream fis;
ObjectInputStream in = null;
try {
fis = new FileInputStream(Environment.getExternalStorageDirectory() + "\\test2.ser");
in = new ObjectInputStream(fis);
char temp = in.readChar();
} catch (IOException e) {
e.printStackTrace();
} finally {
in.close();
}
}
public static void WriteToFile() throws Exception {
try {
OutputStream file = new FileOutputStream(Environment.getExternalStorageDirectory() + "\\test2.ser");
ObjectOutput output = new ObjectOutputStream(file);
try {
output.writeChar('c');
} finally {
output.close();
}
} catch (IOException ex) {
throw ex;
}catch (Exception ex) {
throw ex;
}
}
}
您是否已驗證函數'WriteToFile'是否正常工作? –
顯示來自EOFException的跟蹤。在什麼情況下引發異常? –