BufferedReader does not read all lines from a file

I am trying to read /proc/net/xt_qtaguid/statsin Android 6.
Using the command cat, I get the following:

2 a0 0 0 123456 311 48329 737 48 1
3 b0 0 0 0 0 0 0 0
4 c0 123456 311 48329 737 48 1
5 d0 111111 111 22222 222 33 1

My java code is trying to read the file line by line:

File sysDataFile = new File(PATH_TO_FILE);
BufferedReader bufReader = null;
FileReader fileReader = null;
try {
   fileReader = new FileReader(sysDataFile);
   bufReader = new BufferedReader(fileReader);

   String line = null;
   while ((line = bufferedReader.readLine()) != null) {
       // print to console each line
       System.out.println("Line: " + line);
   }
 } catch (IOException e) {
    System.out.println("IOException thrown!");           
 } finally {
   bufReader.close();
   fileReader.close();
 }

When I run the above code, it only displays the first two lines in the console:

Line: 2 a0 0 0 123456 311 48329 737 48 1
Line: 3 b0 0 0 0 0 0 0 0

Why?

+6
source share
3 answers

I am trying to read /proc/net/xt_qtaguid/statsin Android 6.

Linux /proc/ : procfs, , , ( , ). , , , . : () 0 ( ls -l), .

, , 2 .

"file" xt_qtaguid Android, " / " ".

:

read_proc uid, .

, , , , , "" 2 , ( Android ).

, , , adb cat, , , , . ( , ), .

:

, , , .., UID , TrafficStats.setThreadStatsUid() . android.permission.MODIFY_NETWORK_ACCOUNTING, .

, / TrafficStats.setThreadStatsUid(), , MODIFY_NETWORK_ACCOUNTING.

+13

, cat java, . : cat Android

+2

readLine() '\n'. , '\n', readLine() null . , , .

At this point you can try two things:

  • check the encoding of your system file and the use of your program is the same
  • change this:

    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        // print to console each line
        System.out.println("Line: " + line);
    }
    

    :

    char c = null;
    while ((c = (char)bufferedReader.read()) != null) {
        // print to console each char
        System.out.println("Line: " + c);
    }
    

and see what happens.

-4
source

All Articles