我正在制作一个程序,它可以将输入的单词保存到sd卡中的文本文件中。然而,在我的平板电脑中安装sd卡有一些错误,所以我考虑将文本文件保存到内部存储。有人能帮我把这个转到内部存储器吗?任何评论都是非常appreciated.thank你。
,这是我的代码:
public void writeToSDFile() {
File root = android.os.Environment.getExternalStorageDirectory();
tv.append("\nExternal file system root: "+root);
File dir = new File (root.getAbsolutePath());
dir.mkdirs();
File file = new File(dir, "wordlist.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println(stringword);
pw.append(stringword);
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found.");
} catch (IOException e) {
e.printStackTrace();
}
tv.append("\n\nFile written to "+file);
}//end writeToSDFile发布于 2013-02-18 14:47:19
由于外部存储是可移动的,因此不应该假定它始终存在。因此,在执行任何io操作之前,请检查存储状态。
关于您对内部存储的问题,有两种方法: 1.在应用程序存储(app cache)中--参考: Environment.getDataDirectory() 2。在公共数据目录中,参考: Context.getCacheDir()。
希望这能有所帮助。
发布于 2013-02-18 14:43:08
这应该可以帮助您:http://developer.android.com/guide/topics/data/data-storage.html
https://stackoverflow.com/questions/14938805
复制相似问题