我正在嘗試打開一個現有的 .txt 檔案以遍歷其行。為此,在我的HelloArActivityTest2.java課堂上,我使用了常用的代碼,它使用了一個將檔案作為引數的 Scanner:
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.txt檔案復制到其他專案位置,以查看路徑是否正確。而且我還閱讀了類似論壇上的其他討論,并嘗試了其他方式。但錯誤仍然存??在。你知道會發生什么嗎?為了更清楚地說明,這是我將prov.txt檔案復制到除錯的額外位置的兩個螢屏截圖。
復制檔案(一)
復制檔案(二)
此外,這是我在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();
}
歡迎任何幫助。提前致謝。
uj5u.com熱心網友回復:
您不應將純文本檔案作為檔案夾樹結構中的代碼保留在示例位置。
將要讀取的檔案放在“assets”檔案夾中,改用該getAssets()方法。
有關如何使這項作業的詳細說明,請參閱以下教程:https ://www.tutorialspoint.com/how-to-read-a-file-from-assets-on-android
uj5u.com熱心網友回復:
你可以試試絕對路徑,然后改變獲取路徑的方法
uj5u.com熱心網友回復:
您的代碼的等效使用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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520827.html
