package org.jaberrio.personai2;
import android.content.Context;
import android.widget.Toast;
import java.io.FileOutputStream;
public class DataBaseManager {
public void setDataBase(Context context) {
String fileName = "CoolDataBaseFile.txt";
String inputText = "Random Text Goes Here";
FileOutputStream outputStream;
try {
outputStream = context.openFileOutput(fileName, context.MODE_PRIVATE);
outputStream.write(inputText.getBytes());
outputStream.close();
Toast finishedLoad = Toast.makeText(context, "I Have Finished Loading", Toast.LENGTH_SHORT);
finishedLoad.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
现在我正在尝试创建一个文件,但是当我调用此构造函数时,Toast 通知我它已运行,但没有创建文件。我在 data/data/中查找了它,但我的应用程序目录甚至不在那里。
我还从这样的 fragment 调用构造函数:
DataBaseManager db = new DataBaseManager();
db.setDataBase(getActivity().getApplicationContext());
最佳答案
On a Physical Device
您无法通过普通工具访问实际设备上的内部存储。主要的异常(exception)是您对设备进行 root 操作。
adb shell run-as
允许您运行 shell 命令,就好像它们正在运行您的应用程序一样,因此原则上您可以使用它来检查内部存储和查看该文件是否存在。
更简单的解决方案是使用模拟器,特别是 x86 模拟器,因为您可以使用 DDMS(在 Eclipse 中)或 Android Device Monitor(从 Android Studio 启动)浏览模拟器上的内部存储。
关于java - Android FileOutputStream 不创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30956909/