Airfix Airfix - 7 months ago 8
Java Question

How to referencing my raw txt file with FileReader?

I'm trying to teach myself how to read a raw resources text file into a multidimensional array without much luck thus far. Here is the raw text file:

ken todd
stuart brian
bob stewart
orange apple

green red


The code I have is shown below.

public void LoadFromFile (){

// int fileId = (getResources().getIdentifier("text_file",
// "raw", getPackageName()));
Scanner scan = new Scanner(new FileReader(R.raw.text_file);
// initialises the scanner to read the text_file

String[][] entries = new String[6][2];
// creates a 2d array with 6 rows and 2 columns.

int i = 0;
while (scan.hasNextLine()) {
entries[i] = scan.nextLine().split("\\s+");
i++;
}
//loops through the file and splits on a space

for (int row = 0; row < entries.length; row++) {
for (int col = 0; col < entries[0].length; col++) {
if (entries[row][col] != null) {
System.out.print(entries[row][col] + " ");
}
}
if (entries[row][0] != null) {
System.out.print("\n");
}
}
//prints the contents of the array that are not "null"
}


I'm getting an error on the scanner line. Specifically FileReader(R.raw.text_file) I get


Cannot resolve constructor 'FileReader(int)'


Obviously FileReader is looking for an int and i tried to do that by putting the file ID int an int variable and then using the int variable in the FileReader but that didn't work either.

int fileId = (getResources().getIdentifier("text_file",
"raw", getPackageName()));
Scanner scan = new Scanner(new FileReader(fileId);


It gave me the same error


Cannot resolve constructor 'FileReader(int)'


What am I doing wrong here? Anybody got tips in how to get a file put into an array?

Cheers

Airfix

Answer

Thanks to the guidance of Doug Stevenson I managed to get the code correct to read a file into an array. I've got a couple of other details that I need to sort out such as making the array the correct size for the text file (rather than hard coding) and dealing with a blank line in the text file.

In the mean time here is the working code to load a text file into an array:

public void TextFileReader (){

        String[][] textFileData = new String[5][2];

        InputStreamReader InputSR = null;
        BufferedReader BufferedRdr = null;
        String thisLine = null;

        InputSR = new InputStreamReader(getResources().openRawResource(R.raw.text_file));
        BufferedRdr = new BufferedReader(InputSR);

        try {
            // open input stream text_file for reading purpose.
            int i = 0;
            while ((thisLine = BufferedRdr.readLine()) != null) {
                String[] parts = thisLine.split(" ");
                textFileData[i][0] = parts[0];
                textFileData[i][1] = parts[1];
                Log.v("String Array"+i+"0", String.valueOf(textFileData[i][0]));
                Log.v("String Array"+i+"1", String.valueOf(textFileData[i][1]));
                i = i +1;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }