无法在 Android 应用程序中创建文件

Unable to create file in Android app

我正在制作一个 android 应用程序,我将在其中使用文件处理,但首先我需要创建它,但使用以下代码:

File logf = new File("log1.txt");
Boolean bb = logf.exists();
if(!bb)
try {
bb = logf.createNewFile();
} catch (IOException e) {
msg.setText("not able to create file.");
}

'msg' 是我用来显示错误的 TextView 对象,当我 运行 这个应用程序.. 它进入 IOException e catch.. 如果我正在做某事请告诉我错了还是什么?..此外,我正在为我的应用程序使用 Android 2.2。

您需要提供文件的绝对路径,如下所示。

File file = new File(/data/packagename/files/ + "log1.txt");

在应用存储中,请试试这个:

File file = new File(getFilesDir(), "file.txt");

或者如果你想附加文件名,试试这个:

File file = new File(getFilesDir() + File.separator + "file.txt");