Java – BufferedReader does not read all lines in the file

I tried to read / proc / net / XT in Android 6_ qtaguid / stats.

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 tries 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 prints out only 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?

Solution

In Linux, files under / proc / are not actually files: they are processed by PROCFS, a special file system that executes code every time a file entry is read or written (the code is a callback function defined in the kernel module) Therefore, these pseudo files are not static like conventional files, but completely dynamic Size provides an interesting clue: (most) these files have a length of 0 (as shown in LS - L), but they display some content when reading

In short, reading the same file from two different contexts can be expected to produce two different results

In this case, the "file" callback is called by XT_ Qtaguid module for Android processing, which manages "per application / delegate data usage monitoring"

This answer says:

The first part is a little vague, but it seems to indicate that the difference is based on the user ID, and the module will only "print" 2 lines of data when the regular application reads this file (please note that Android assigns a unique user ID to each application and runs it as that user in a separate process)

You didn't provide enough details, but I must assume that when you print it from ADB using cat, you may not have the same user ID and permissions as when you try to read it from the application I didn't track the exact implementation details (if you want, the source for this module can be read here), but other variables may work

The doc says:

Therefore, processes / applications can use trafficstats Setthreadstatsuid() to get more lines from the file, but this requires modify_ NETWORK_ Accounting permission

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>