尝试在 java 中打开 .txt 文件时出现异常

Exception when trying to open a .txt file in java

提问人:Borja Davó 提问时间:10/25/2022 最后编辑:Borja Davó 更新时间:10/25/2022 访问量:58

问:

我正在尝试打开一个现有的.txt文件以遍历其行。为此,在我的课堂上,我使用了常用的代码,该代码使用将文件作为参数的 Scanner:HelloArActivityTest2.java

File myObj = new File("app/src/main/assets/models/prov.txt");
Scanner myReader = new Scanner(myObj);
int counter = 0;
while (myReader.hasNextLine() && counter <= 10) {
    String data = myReader.nextLine();
    System.out.println(data);
    counter ++;
}
myReader.close();

但是我收到此错误:

2022-10-25 12:49:56.172 12110-12132/com.google.ar.core.examples.java.helloar W/System.err: java.io.FileNotFoundException: C:/Users/serer/Desktop/Proyecto02/project_name/project_name/app/src/main/assets/models/prov.txt: open failed: ENOENT (No such file or directory)
2022-10-25 12:49:56.172 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:492)
2022-10-25 12:49:56.172 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:160)
2022-10-25 12:49:56.172 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at java.util.Scanner.<init>(Scanner.java:620)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at com.google.ar.core.examples.java.helloar.HelloArActivityTest2.helloArActivityTest(HelloArActivityTest2.java:210)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:549)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.rules.RunRules.evaluate(RunRules.java:20)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
2022-10-25 12:49:56.173 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
2022-10-25 12:49:56.174 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:162)
2022-10-25 12:49:56.174 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.Suite.runChild(Suite.java:128)
2022-10-25 12:49:56.174 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.Suite.runChild(Suite.java:27)
2022-10-25 12:49:56.174 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
2022-10-25 12:49:56.174 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
2022-10-25 12:49:56.174 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
2022-10-25 12:49:56.176 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
2022-10-25 12:49:56.176 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
2022-10-25 12:49:56.176 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
2022-10-25 12:49:56.176 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
2022-10-25 12:49:56.176 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
2022-10-25 12:49:56.176 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
2022-10-25 12:49:56.177 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:444)
2022-10-25 12:49:56.177 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2205)
2022-10-25 12:49:56.179 12110-12132/com.google.ar.core.examples.java.helloar W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
2022-10-25 12:49:56.180 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at libcore.io.Linux.open(Native Method)
2022-10-25 12:49:56.180 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
2022-10-25 12:49:56.180 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
2022-10-25 12:49:56.180 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
2022-10-25 12:49:56.180 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)
2022-10-25 12:49:56.180 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:478)
2022-10-25 12:49:56.180 12110-12132/com.google.ar.core.examples.java.helloar W/System.err:  ... 33 more

我试图将文件复制到其他项目位置,以查看路径是否正确。我也在这样的论坛上阅读了其他讨论,并尝试了其他方法。但错误仍然存在。你知道会发生什么吗?为了进一步澄清,这是我复制文件进行调试的额外位置的两张屏幕截图。prov.txtprov.txt

复制的文件 (I)

复制的文件 (II)

此外,这是我在课堂上使用的代码,以及我所做的所有尝试:HelloArActivityTest2.java

try {
    //File myObj = new File("app/assets/models/prov.txt");
    //File myObj = new File("app/src/main/assets/models/prov.txt");
    //File myObj = new File("prov.txt");
    //File myObj = new File(new File("app/src/main/assets/models/prov.txt").getAbsolutePath());
    //File myObj = new File(path.trim());
    //File myObj = new File(Objects.requireNonNull(HelloArActivityTest2.class.getResource("app/src/main/assets/models/prov.txt")).toURI());

    //System.out.println("user.dir = " + System.getProperty("user.dir"));
    //String path = System.getProperty("user.dir") + "src/androidTest/prov.txt";
    //String path = System.getProperty("user.dir") + "\\src\\androidTest\\prov.txt";
    //MockHttpServletRequest request = new MockHttpServletRequest();
    //String path = request.getSession().getServletContext().getRealPath("/");

    File myObj = new File("app/src/main/assets/models/prov.txt");

    //myObj.setReadable(true);
    //myObj.setWritable(true);

    System.out.println("myObj.mkdirs() = " + myObj.mkdirs());   //false
    System.out.println("myObj.exists() = " + myObj.exists());   //false
    System.out.println("myObj.isDirectory() = " + myObj.isDirectory()); //false
    System.out.println("myObj.canRead() = " + myObj.canRead()); //false
    System.out.println("Current directory: " + new File(".").getAbsolutePath());    // /.

    Scanner myReader = new Scanner(myObj);
    int counter = 0;
    while (myReader.hasNextLine() && counter <= 10) {
        String data = myReader.nextLine();
        System.out.println(data);
        counter ++;
    }
    myReader.close();

} catch (FileNotFoundException e) {
    System.out.println("An error occurred.");
    e.printStackTrace();
}

欢迎任何帮助。提前致谢。

Java 文件 路径 java.util.scanner fileNotFoundException

评论

0赞 Stultuske 10/25/2022
它无法打开该文件,因为它找不到该文件。文件不存在,或者需要调整路径
0赞 Christoph S. 10/25/2022
路径/文件名是否正确?C:/Users/serer/Desktop/Proyecto02/project_name/project_name/app/src/main/assets/models/prov.txt

答:

0赞 damianr13 10/25/2022 #1

不应将纯文本文件保留在示例位置,作为文件夹树结构中的代码。

将要读取的文件放在“assets”文件夹中,然后改用该方法。getAssets()

有关如何实现此工作的详细说明,请参阅以下教程:https://www.tutorialspoint.com/how-to-read-a-file-from-assets-on-android

0赞 pageqiu009 10/25/2022 #2

你可以试试绝对路径,然后改变获取路径的方法

0赞 g00se 10/25/2022 #3

等效的 U 代码可以是getAssets

try (BufferedReader in = new BufferedReader(new InputStreamReader(getAssets().open("prov.txt")))) {
    in.lines().limit(10).forEach(System.out::println);
}
catch(IOException e) {
    e.printStackTrace();
}