Home > Net >  java.io.filenotfoundexception: open failed: enoent (no such file or directory). Opening file from di
java.io.filenotfoundexception: open failed: enoent (no such file or directory). Opening file from di

Time:11-16

I need to get text from files in directory Tsks. Every file has name "tasks" class (1 or 2) random symbol from specifications[] I tried to open file in

\\app\\src\\main\\res\\Tasks\\

and I can't do this. Below is my code.

public class Task1 extends AppCompatActivity {
final Random random = new Random();
public String specifications[] = new String\[\]{"1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e" , "f"};
public String[] linii = new String[5];
public void main(String[] args) throws IOException {
Bundle arguments = getIntent().getExtras();
int clasik = arguments.getInt("class") - 2131230843;
FileInputStream fstream = new FileInputStream("D:\\Project\\Proektorat\\app\\src\\main\\res\\Tasks\\tasks"   clasik   specifications[random.nextInt(15)]   ".txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
int i = 0;
while ((br.readLine()) != null){
linii[i]  = br.readLine();
i  = 1;
}

And errors:

W/System.err: java.io.FileNotFoundException: D:\\Project\\Proektorat\\app\\src\\main\\res\\Tasks\\tasks1a.txt: open failed: ENOENT (No such file or directory)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:574)
W/System.err:     at java.io.FileInputStream.\<init\>(FileInputStream.java:160)
W/System.err:     at java.io.FileInputStream.\<init\>(FileInputStream.java:115)
W/System.err:     at com.example.project.Task1.main(Task1.java:26)
W/System.err:     at com.example.project.Task1.onCreate(Task1.java:41)
W/System.err:     at android.app.Activity.performCreate(Activity.java:8290)
W/System.err:     at android.app.Activity.performCreate(Activity.java:8269)

CodePudding user response:

You are accessing your computers resources, which is inaccessible from your phone/emulator.

Take a look at this introduction on how to work with resources. Especially the part Accessing original files might be of interest for you.

Alternatively you can use the phone's/emulator's file system and upload a file with device file explorer (it's a Linux system, just in case you aren't familiar with)

  • Related