1

I've got the file, which can vary in size, in terms of lines. The only thing I know is that it's made of same modules of, lets say, 7 lines. So it means that .txt can be 7, 14, 21, 70, 77 etc. lines. I need to get only header of each module - line 0, 7 and so on.

I've written this code for the job:

    textFile = new File(Environment.getExternalStorageDirectory() + "/WorkingDir/" + "modules.txt" );

    List<String> headers = new ArrayList<>();

    if (textFile.exists()) {
        try {
            FileInputStream fs= new FileInputStream(textFile);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fs));
            int lines = 0;
            int headLine = 0;
            while (reader.readLine() != null) { lines++;}
            Log.i("Debug", Integer.toString(lines));
            while(headLine < lines){


                for (int i = 0; i < dateLine - 1; i++)
                {
                    reader.readLine();
                    Log.i("Debug", reader.readLine());
                }
                headers.add(reader.readLine());


                headLine += 7;
            }

            Log.i("Debug", headers.toString());


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

The problem is that it always returns [null]. I do not know where's the problem, since I used similar questions from overflow as references.

Oleksandr Firsov
  • 1,428
  • 1
  • 21
  • 48
  • http://stackoverflow.com/questions/2312756/how-to-read-a-specific-line-using-the-specific-line-number-from-a-file-in-java – sasikumar Mar 05 '16 at 11:36
  • I've read that question, that's where I got the general idea. http://stackoverflow.com/a/2312789/4491661 - in particular – Oleksandr Firsov Mar 05 '16 at 11:38
  • you get a buffered reader to the file like this `BufferedReader br = new BufferedReader(new FileReader(file))` , then `line = br.readLine()` – Bhargav Mar 05 '16 at 11:40

1 Answers1

2
ArrayList<String>  headerLines = new ArrayList();
BufferedReader br = new BufferedReader(new FileReader(file));
try {
    String line;
    int lineCount = 0;
    while ((line = br.readLine()) != null) {
       // process the line.
       if(lineCount % 7 == 0) {
           heaaderLines.add(line);
       }
       lineCount ++;
    }
} catch (IOException ioEx) {
    ioEx.printStackTrace();
} finally {
    br.close();
}
Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • this one gets the job done. Although it says that `Try-with-resources requires API level 19`. I suppose it will conflict with earlier devices. Is there a way to fix that? – Oleksandr Firsov Mar 05 '16 at 11:50
  • yes, I didn't know you your minSdkVersion so I chose the way with least amount of code, ill modify it with the older java vesion's style of handling resources – Bhargav Mar 05 '16 at 11:56
  • @OleksandrFirsov just edited my answer, try-with-resources was introduced in Java 8, so only android api version 19+ supports java hence the warning – Bhargav Mar 05 '16 at 12:00
  • 1
    Thanks for the time and answer! – Oleksandr Firsov Mar 05 '16 at 12:10