1

When I try to read some files in my Android app they appear to not exist, here is my code:

public class ReadFiles1 {

    //Static Scanner and File Objects
    static Scanner s;
    

    // Static method that returns an ArrayList
    static ArrayList<String> words (String filename){

        //Instantiate File with file name within parameters
        File n = new File(filename);

        //Instantiate Scanner s with f variable within parameters
        //surround with try and catch to see whether the file was read or not
        try {
            s = new Scanner(n);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
            System.out.println("Problem here");
        }

        //Instantiate a new ArrayList of String type
        ArrayList<String> theWord = new ArrayList <String>();

        //while it has next ..
        while(s.hasNext()){
            //Initialise str with word read
            String str=s.next();

            //add to ArrayList
            theWord.add(str);

        }
        //return ArrayList
        return theWord;

    }

I don't know what the problem is, I put the txt files in the same package as the .java files. This is the error I get when runnning this: check this link for PICTURE(https://ibb.co/cn3QCv)

W/System.err: java.io.FileNotFoundException: build/numbers.txt: open failed: ENOENT (No such file or directory)
Jack D.
  • 31
  • 6

1 Answers1

1

Don't put your text files in java folder instead put it in

app/src/main/res/raw/ folder and read it in program using below code.

static ArrayList<String> words (InputStream in) {
    try {
        s = new Scanner(in);
    } catch (FileNotFoundException e) {

        e.printStackTrace();
        System.out.println("Problem here");
    }

and call this method from any activity or service using below code

InputStream in = getResources().openRawResource(R.raw.numbers);
ArrayList<String> words = ReadFiles1.words(in);

or if you're calling this method from any other class then should have context reference

InputStream in = context.getResources().openRawResource(R.raw.numbers);
ArrayList<String> words = ReadFiles1.words(in);

Hope this will work.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • I got this error message: W/System.err: java.io.FileNotFoundException: /storage/emulated/0/numbers.txt: open failed: ENOENT (No such file or directory) – Jack D. Jul 05 '17 at 08:06
  • I don't understand why the files are not visible, they are in the same package, in Eclipse I didn't have any problem... – Jack D. Jul 05 '17 at 08:11
  • I just want to make clear, the txt files are in the app, not in an external stotrage, I'm just saying because of the suggestion .getExternalStorageDirectory() – Jack D. Jul 05 '17 at 08:18
  • Where you put that txt file..if you can show project structure then I can tell you the cause – ELITE Jul 05 '17 at 08:37
  • try [read file from project structure](https://stackoverflow.com/questions/9544737/read-file-from-assets) – ELITE Jul 05 '17 at 08:42
  • I changed the answer...check once – ELITE Jul 05 '17 at 09:18
  • @ELIOT unfortunately I couldn't do it...What I am trying to do is read a text file, put every word in an arraylist and use it as needed later. This is very easy in Eclipse ,but challenging for me in Android Studio, I don't know why... – Jack D. Jul 05 '17 at 09:45
  • Is there a simple way to import my java code from eclipse into android studio and use the functions that I have written in my eclipse java code for android studio as well? – Jack D. Jul 05 '17 at 09:53