I am trying to open an existing .txt file to iterate through its lines. To do this, in my HelloArActivityTest2.java
class, I have used the commonly used code, that uses a Scanner that takes a file as a parameter:
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();
But I get this error:
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
I have tried to copy the prov.txt
file to other project locations, to see if the path was taken properly. And I also read other discussions on forums like this, and tried other ways. But the error still persists. Do you know what could be happening? To clarify it more, these are two screenshots of extra places where I copied the prov.txt
file to debug.
Also, this is the code that I am using in my HelloArActivityTest2.java
class, with all the tries I did:
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();
}
Any help is welcome. Thanks in advance.
CodePudding user response:
You should not be keeping plain text files in the sample place as the code in your folder tree structures.
Put the files you want to read in the "assets" folder and use the getAssets()
method instead.
See the following tutorial for a detailed explanation of how to make this work: https://www.tutorialspoint.com/how-to-read-a-file-from-assets-on-android
CodePudding user response:
you can try the absolute path,then change the method of get path
CodePudding user response:
The equivalent using getAssets
of you code could be
try (BufferedReader in = new BufferedReader(new InputStreamReader(getAssets().open("prov.txt")))) {
in.lines().limit(10).forEach(System.out::println);
}
catch(IOException e) {
e.printStackTrace();
}