0

I am basically trying to read a long list of numbers(doubles)from a text file and save them into an array. I have these lines of code but it doesn't work when I load into my android smartphone. The readfile() does work completely when I use debug mode to check if my code reads the ExamScore, it does read and store the values as expected in my laptop. When it loads into smartphone, it just doesn't work. I save my ExamScore.txt in the root directory of android studio, for example, Users->AndroidStudioProjects->Project A. The main concern I have is that:

  1. How do I know if this ExamScore.txt is saved into my smartphone as well when I build the app? Do I have to save the text file into my smartphone separately or something?The error I get is

java.io.FileNotFoundException: ExamScore.txt: open failed: ENOENT (No such file or directory)

static double[] readfile() throws FileNotFoundException{

    Scanner scorefile = new Scanner(new File("ExamScore.txt"));
    int count = -1;
    double[] score = new double[8641];
    while (scorefile.hasNext()) {
        count = count + 1;
        score[count] = Double.parseDouble(scorefile.nextLine());
    }
    scorefile.close();
    return score;
}

In my main code,

double []score=readfile();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
cmy113
  • 33
  • 8
  • Android isn't going to run the exact same as a Java app on your computer. – OneCricketeer Mar 12 '17 at 19:52
  • Possible duplicate of [How do I read the file content from the Internal storage - Android App](http://stackoverflow.com/questions/14768191/how-do-i-read-the-file-content-from-the-internal-storage-android-app) – Machavity Mar 12 '17 at 22:56

2 Answers2

2

I save my ExamScore.txt in the root directory of android studio, for example, Users->AndroidStudioProjects->Project A... How do I know if this ExamScore.txt is saved into my smartphone as well when I build the app?

It isn't.

You need to create an assets folder.

Refer: Where do I place the 'assets' folder in Android Studio?

And you would use getAssets() to read from that folder.

public class MainActivity extends Activity {

    private double[] readfile() throws FileNotFoundException{

        InputStream fileStream = getAssets().open("ExamScore.txt");
        // TODO: read an InputStream

    }
}

Note: that is a read-only location of your app.

Or you can use the internal SD card.

How do I read the file content from the Internal storage - Android App


EDIT With refactored code in other answer

public static List<Double> readScore(Context context, String filename)  {

    List<Double> scores = new ArrayList<>();

    AssetManager mgr = context.getAssets();
    try ( 
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(mgr.open(fileName)));
    ) {
        String mLine;
        while ((mLine = reader.readLine()) != null) {
             scores.add(Double.parseDouble(mLine));
        }
    } catch (NumberFormatException e) {
        Log.e("ERROR: readScore", e.getMessage());
    }
    return scores;
}

And then

List<Double> scores = readScore(MainActivity.this, "score.txt");
Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Hey @cricket_007 Does it mean that if I saved into the assets folder in my laptop, it will be sort of "pushed" into my android smartphone when I build the app? – cmy113 Mar 12 '17 at 19:59
  • It should, yes. That `assets` directory will be part of the APK file installed to your app. It can make the filesize very large depending on what you put in there, so there might be a size limit. – OneCricketeer Mar 12 '17 at 20:04
  • but it gives me error cannot resolve method getAssets() after I create the asset folder and using the code you mentioned. – cmy113 Mar 12 '17 at 20:06
  • Then you don't have that code in an Activity... or you need to remove `static` on the method – OneCricketeer Mar 12 '17 at 20:10
0

For those who are wondering, this is my solution! Thank you all for your help!!!! The issue I had was I didn't write it in the main activity but wrote the code in other java file. After writing this in the main activity file and putting my text file inside the assets folder. The issue is resolved :

public static LinkedList<Double> score=new LinkedList<Double>();
public  void readScore() throws java.io.IOException {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(getAssets().open("score.txt")));
    String mLine;
    while ((mLine = reader.readLine()) != null) {
         score.add(Double.parseDouble(mLine));
    }
    reader.close();
}
cmy113
  • 33
  • 8
  • See the edit to my answer for a "better" version of this. – OneCricketeer Mar 13 '17 at 17:53
  • Thanks @cricket_007 ,do appreciate your effort!! just a quick question. Would you teach me how to trigger readScore everytime my app starts? The problem with me right now is that I need the score to be passed into the other function but my current function is that when a button is clicked , then only it will trigger List scores = readScore(MainActivity.this, "score.txt");. My app has error now since the list is always null if it's not initiated! I hope you understand what I mean. – cmy113 Mar 13 '17 at 18:18
  • You can call this method in an overridden `onStart()` method (or just `onCreate()`) of the activity you need it in – OneCricketeer Mar 13 '17 at 18:21
  • Dear @cricket_007, thank you so much for your help! My issue has now been resolved :) – cmy113 Mar 13 '17 at 18:35