文件未找到,但确实存在

时间:2014-07-08 03:18:17

标签: java android file-io

我无法访问我知道存在于BufferedReader中使用它的文件。 这是代码:

File file = new File(Environment.DIRECTORY_DOWNLOADS + getString(R.string.dloaded_latest_numbers_dir) + getString(R.string.dloaded_latest_numbers_file));
BufferedReader buffedReader = new BufferedReader(new FileReader(file));

但每当我点击BufferedReader行时,我得到" java.io.FileNotFoundException:/Download/MyDir/MyFile.txt:open failed:ENOENT(没有这样的文件或目录)"

我在TextView中显示文件位置:

TextView displayFileLocation = (TextView) findViewById(R.id.readLocTextView);
displayFileLocation.setText("location is: " + file.getAbsolutePath());

显示文件的正确完整路径。 (/Download/MyDir/MyFile.txt)

我的AndroidManifest.xml文件具有以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

logcat输出是:

07-08 13:06:28.556  15351-15351/au.com.acent.ash.basiclottochecker D/OpenGLRenderer﹕ Enabling debug mode 0
07-08 13:06:43.451  15351-15351/au.com.acent.ash.basiclottochecker W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ java.io.FileNotFoundException: /Download/MyDir/MyFile.txt: open failed: ENOENT (No such file or directory)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:420)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.io.FileReader.<init>(FileReader.java:42)
07-08 13:06:57.925  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:64)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.Posix.open(Native Method)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:404)
07-08 13:06:57.935  15351-15351/au.com.acent.ash.basiclottochecker W/System.err﹕ ... 17 more

我绝对肯定这个文件存在,我使用DownloadManager使用目录位置完全相同的位置参考将此文件下载到完全相同的位置:

File file = new File(Environment.DIRECTORY_DOWNLOADS + getString(R.string.dloaded_latest_numbers_dir));

我做错了什么? 请帮忙!

1 个答案:

答案 0 :(得分:1)

您的代码正在尝试访问/Download/MyDir/MyFile.txt,这看起来不是正确的位置,因为该文件不存在于根文件夹中。它可能位于sdcard目录中。要获得正确的目录,请使用

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

而不是

Environment.DIRECTORY_DOWNLOADS