如何將類似mEditText.getText();
的可編輯對象保存到文件中? 我試着用下面的代碼,它的作品,但最終我得到一個IOException
和IOException.getLocalizedMessage();
和IOException.getMessage();
都顯示以下字符串。將可編輯對象保存爲文件
E/Error:(5223): android.text.SpannableStringBuilder
這裏是我試過的代碼:
try {
SpannableStringBuilder ssb = new SpannableStringBuilder(mMainEditText.getText());
//Create a File object with user entered file name...
File outputFile = new File(getDocStorageFolder(),
mUserEnterFileName
+ ".msd");
Log.e("Path:", "" + outputFile.getAbsolutePath());
Toast.makeText(MainActivity.this, "" + outputFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
FileOutputStream fos = new FileOutputStream(outputFile); //create your FileOutputStream here
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(ssb);
oos.close();
oos.flush();
fos.close();
Toast.makeText(MainActivity.this, "Success!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Log.e("Error: ", e.getMessage());
Log.e("Error: ", e.getLocalizedMessage());
Toast.makeText(MainActivity.this, "Error occured while "
+ "attempting to create the Document file!", Toast.LENGTH_LONG).show();
}
沒有任何與Android的spanned-string邏輯相關的支持'Serializable'。 – CommonsWare
謝謝。那麼,你能否提出一些其他的可能性來完成這項工作? – mifthi