0

I am trying to open a text file with the following code:

try{
   FileReader fr=new FileReader("text\\test.txt");
   BufferedReader br=new BufferedReader(fr);
    
   int i;
   while((i=br.read())!=-1){
      System.out.print((char)i);
   }
   br.close();
   fr.close();
} catch (IOException e){
   e.printStackTrace();
}

This code results in this error (line 69 being the FileReader initialization):

W/System.err: java.io.FileNotFoundException: text\test.txt: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:492)
        at java.io.FileInputStream.<init>(FileInputStream.java:160)
        at java.io.FileInputStream.<init>(FileInputStream.java:115)
        at java.io.FileReader.<init>(FileReader.java:58)
        at com.example.mytrial.MainActivity.onCreate(MainActivity.java:69)

This is what my file system looks like: here

Any and All help is appreciated!

Abra
  • 19,142
  • 7
  • 29
  • 41
user21
  • 3
  • 3

1 Answers1

0

If your file is in your project class path you should able to drop text//

If it's inside the resources you can access it with text\ Refer below for patterns https://mkyong.com/java/java-read-a-file-from-resources-folder/

Occlumency
  • 149
  • 2
  • 9