【问题标题】:How to check whether a file is present or not in the internal file system of android [duplicate]如何检查文件是否存在于android的内部文件系统中[重复]
【发布时间】:2012-09-19 10:32:26
【问题描述】:

可能重复:
Test if file exists

我在我的 android 应用程序内部文件系统的 /files 文件夹中存储了一个文件。我想检查它是否以编程方式存储在那里。我该如何检查?请帮助我。

File file = getBaseContext().getFileStreamPath("Android.mk"); if(file.exists()) { Toast.makeText(this,"Android.mk exist", Toast.LENGTH_LONG).show(); }

提前致谢。

【问题讨论】:

  • file.exists() 是要走的路
  • 文件文件 = getBaseContext().getFileStreamPath("Android.mk"); if(file.exists()) { Toast.makeText(this,"Android.mk 存在", Toast.LENGTH_LONG).show(); }
  • 我已经尝试过了,我通过在 android 应用程序本身中创建文件夹来存储该文件。不在 sd 卡或其他地方。

标签: android file-io


【解决方案1】:

如果我像 Android dev-tut (Link) 中描述的那样在内部存储中写入文件到我的应用程序私有存储,我可以通过代码验证文件已成功写入!

所以验证码应该没问题。也许您编写文件的方式是错误的。这是我的代码 sn-p:

String FILENAME = "Android.mk";
String string = "hello world";

FileOutputStream fos = openFileOutput(FILENAME, getBaseContext().MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

File file = getBaseContext().getFileStreamPath("Android.mk");
if (file.exists()) {
    Toast.makeText(this, "Android.mk exist", Toast.LENGTH_LONG).show();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-11
    • 2017-06-14
    • 2012-08-16
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多