我正在创建一个程序,将EditText中输入的单词保存到SD卡中的文本文件中。但是,在我的平板电脑上安装SD卡有一些问题,所以我想把文本文件保存到内部存储器。任何人都可以帮我如何切换到内部存储?任何评论都会非常感谢。谢谢。
这是我的代码:
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
答案 0 :(得分:2)
答案 1 :(得分:0)
由于外部存储是可移动的,因此您不应该假定它一直存在。因此,在执行任何io操作之前,请检查存储状态。
关于您对内部存储的问题,有两种方法: 1.在应用程序存储(app cache)中 - 参考:Environment.getDataDirectory() 2.在通用数据目录中 - 参考:Context.getCacheDir()。
希望这会有所帮助。