创私有的应用程序文件openFileInput()和openFileOutput()

Android提供了openFileInput()和openFileOutput()两种方法来简化,向应用程序沙箱”读取“和”写入“数据流。这两个方法只支持当前应用程序文件夹中的文件,否则将会抛出异常。

String fileName=”myTest.temp“;

//创建一个新的文件输出流,他是应用程序私有的。

FileOutputStream fos=openFileOutput(fileName,Context.MODE_PRIVATE);

FileOutputStream 创建问件时,如果文件不存在则创建,如果文件已经存在那么将会覆盖原文件。想在已存在文件末尾添加内容,可以改成Context.MODE_APPEND;

通过getFilesDir可以找到存储在应用程序沙箱中使用openFileOutput所创建文件的绝对路径。

File file=getFilesDir();

//文件输入流

FileInputStream fis=openFileInput(fileName);

原文地址:https://www.cnblogs.com/ywtk/p/3797306.html