Question

I had taken 10 lines in txt file which is in assets folder (assets/question.txt), From below code I am able to get Line by line from starting of txt file in assets folder. But I want to start from 4 line of txt file. Please help.

BufferedReader reader_ques;
        try {
            reader_ques = new BufferedReader(new InputStreamReader(getAssets().open("question.txt")));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

try {
            line_q = reader_ques.readLine();

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

        if (line_q != null) 
        {           
            question_tv.setText(line_q);
}
Was it helpful?

Solution

The simplest way is to read it, ignoring them. For instance:

   try {
      for(int i=0 ;i < 4; i++) {
         reader_ques.readLine();
      }
      while ((line_q = reader_ques.readLine()) != null) {
            // do something with line_q
       }


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

or you can have an ArrayList of string:

ArrayList<String> fileContent = new ArrayList<String>();
 try {
       while ((line_q = reader_ques.readLine()) != null) {
               fileContent.add(line_q);
        }
 }

in the second case, you have the first row of the file at index 0 of the ArrayList and so on

if you want the 5th String you just do

String myContent = fileContent.get(5)

OTHER TIPS

String line = FileUtils.readLines(file).get(lineNumber); 

try this..

if you have bufferReader then try following

LineIterator lineIterator = IOUtils.lineIterator(reader_ques);
 for (int lineNumber = 0; lineIterator .hasNext(); lineNumber++) {
    String line = (String) lineIterator .next();
    if (lineNumber == expectedLineNumber) {
        return line;
    }
 }

to use above code you need this library-http://commons.apache.org/proper/commons-io/download_io.cgi

or this-->http://www.motorlogy.com/apache//commons/io/binaries/commons-io-2.4-bin.zip

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top