gpt4 book ai didi

java - 找不到文件,但确实存在

转载 作者:行者123 更新时间:2023-11-30 02:48:26 26 4
gpt4 key购买 nike

我在访问一个我知道存在于 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 (No such file or directory)”

我在 TextView 中显示文件位置:

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

显示文件的正确完整路径。 (/下载/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));

我做错了什么??请帮忙!

最佳答案

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

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

代替

Environment.DIRECTORY_DOWNLOADS

关于java - 找不到文件,但确实存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24623151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com