Using BufferedReader to read one line in Java

I have successfully read the code from CSV. However, when I try to use a fileReader

solo line to read, it makes my code stop working.

Here is my code:

try {
    String line = "";
    fileReader = new BufferedReader(new FileReader(filename));

    while ((line = fileReader.readLine()) != null) {
        String[] tokens = line.split(DELIMITER);
        for (String token : tokens) {
            totalData.add(token);
            if (!artists.contains(token)) {
                artists.add(token);
            }
        }
        for (int l = 0; l <= 999; l++) {
            lineData = fileReader.readLine();
            lineArray[l] = lineData;
        }

    }
} finally {
    fileReader.close();
}

      

When I try to read the dimensions arrayList

and print the data obtained from arrayList

above this code below it stops working:

for (int l = 0; l <= 80; l++) {
    lineData = fileReader.readLine();
    lineArray[l] = lineData;
}

      

If I comment on this for the loop, everything is fine. I really need this for a loop, how can I change my code to solve this problem? Also, what's going on?

+3


source to share


1 answer


for (int l = 0; l <= 80; l++) {
    lineData = fileReader.readLine();
    lineArray[l] = lineData;
}

      

This hard code can replace one line of code:

lineArray[i++] = line;

      



I have adjusted your code and what happened:

String line = "";
int i = 0;
try (BufferedReader fileReader = new BufferedReader(new FileReader(""))) {
    while ((line = fileReader.readLine()) != null) {
        lineArray[i++] = line;
        String[] tokens = line.split(DELIMITER);
        for (String token : tokens) {
            totalData.add(token);
            if (!artists.contains(token)) {
                artists.add(token);
            }
        }
    }
}

      

+1


source







All Articles