【问题标题】:Cannot Find and Read Text File on Android Studio (Java)无法在 Android Studio (Java) 上查找和读取文本文件
【发布时间】:2020-07-25 22:11:38
【问题描述】:

所以我正在使用 Android Studio 创建一个移动应用程序,它允许用户随机化并生成一个字符。我正在尝试逐行读取字符名称的 .txt 文件,以便我可以填充一个包含所有名称的 ArrayList。我试图读取 .txt 文件的所有尝试都没有奏效,控制台总是声称该目录和文件不存在。我曾尝试过多种方式使用 BufferedReader、InputStreamReader 和 Scanner,如下所示:


ArrayList<String> a = new ArrayList<>();

try {

            File file = new File(fileName);

            InputStream in = new FileInputStream(file);

            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String curLine = br.readLine();
            while (curLine != null){
                a.add(curLine);
                curLine = br.readLine();
            }

        }
        catch (Exception e){

        }

还有……


ArrayList<String> a = new ArrayList<>();

try (
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
                ){
            String curLine = reader.readLine();
            for (int i = 1; i != lineNum; i++){
                a.add(curLine);
                curLine = reader.readLine();
            }
            return curLine;
            }
            
        
        catch (Exception e){
           
        }

还有……


ArrayList<String> a = new ArrayList<>();

try {
            File myObj = new File(fileName);
            System.out.println(myObj.getAbsolutePath());
            Scanner myReader = new Scanner(myObj);
            while (myReader.hasNextLine()) {
                a.add(myReader.nextLine());
            }
            myReader.close();
        } catch (Exception e) {

        }

尽管它们都无法正常工作。我尝试使用 file.getAbsolutePath() 显示绝对路径并且路径是正确的。我尝试将文件与 src 文件夹平行放置在与 src 文件夹平行的文件夹中,在 res 文件夹中等等。此外,可能需要注意的是,我通过调用 FileIO 类中的静态方法来运行这些行,在该类中我将字符串文件名作为参数传递。有什么我想念的吗?当我在 NetBeans 或 Eclipse 中执行此操作时,我从文本文件中读取没有问题,事实上,当我在 Android Studio 中尝试此操作时,我在 NetBeans 中的一些旧项目已成为我的模板。

这是我第一个使用 Android Studio 的项目,所以我很可能做错了什么,所以我们将不胜感激!

注意:我知道我在提供的代码中捕获异常的约定效率不高,但是当我尝试使用 FileNotFoundException 捕获异常时,Android Studio 中的调试器由于某些奇怪的原因无法工作。

【问题讨论】:

    标签: java android text bufferedreader fileinputstream


    【解决方案1】:

    你可以试试这个吗:

    ArrayList<String> a = new ArrayList<>();
    File file = new File(filename);
    
    StringBuilder text = new StringBuilder();
    
    try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
    
            while ((line = br.readLine()) != null) {
                a.add(myReader.nextLine());
             }
            br.close();
        }
        catch (IOException e) { // OR CATCH AT LEAST EXCEPTION IF IT DOESNT WORK FOR FILEIOEXCEPTION
         // HERE LOG YOUR ERROR AND GIVE IT TO US
        }
    

    确保您的“文件名”不是目录。 通过记录一些断言的结果来测试你的文件:

    System.out.println("file exists? " + file.exists());
    System.out.println("file is Dir? " + file.isDirectory());
    System.out.println("file can be read? " + file.canRead());
    

    然后给我们结果。

    【讨论】:

    • 当我的 characters.txt 与我的 src 文件夹平行时,我运行了这些确切的行。结果如下: I/System.out:文件是否存在? false I/System.out:文件是 Dir?可以读取虚假文件吗? false java.io.FileNotFoundException: characters.txt (没有这样的文件或目录)
    • 好的,所以请确保您记录的路径完全正确。将您的文件实例替换为 File file = new File(path.trim()) 以确保路径是您想要的路径,路径中没有空格或其他任何内容。另外,首先采用绝对路径而不是相对路径。一旦它起作用,就抓住相对的。
    • 通过 path.trim(),你的意思是 fileName.trim(),对吗?如果是这样,我也试过了,绝对路径完全正确,尽管它仍然说文件不存在。我开始认为它给了我错误的错误信息,因为它可能与权限有关。
    猜你喜欢
    • 2016-06-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2018-01-18
    • 2012-01-20
    • 1970-01-01
    • 2015-08-19
    • 2021-08-03
    相关资源
    最近更新 更多