我正在尝试在 Android 6 中读取 /proc/net/xt_qtaguid/stats。
使用 cat 命令,我得到了这个:
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
我的 java 代码尝试逐行读取文件:
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();
}
当我运行上面的代码时,它只在控制台打印出前两行:
Line: 2 a0 0 0 123456 311 48329 737 48 1
Line: 3 b0 0 0 0 0 0 0 0
为什么?
最佳答案
I am trying to read
/proc/net/xt_qtaguid/statsin Android 6.
在 Linux 中,/proc/ 下的文件实际上不是文件:它们由 procfs 处理,这是一个特殊的文件系统,每次进入文件时都会执行代码被读取或写入(代码是内核模块中定义的回调函数)。所以那些伪文件不像普通文件那样是静态的,而是完全动态的。大小给出了一个有趣的线索:(大部分)这些文件的长度为 0(如 ls -l 所示),但在读取时它们会显示一些内容.
简而言之,从 2 个不同的上下文读取同一个文件会产生 2 个不同的结果是可以预期的。
在这种情况下,"file"回调由 xt_qtaguid module for Android 处理,它管理“每个应用程序/委托(delegate)的数据使用监控”。
this virtual file's read_proc function limit the uid, every application can only read the header and its own line.
第一部分有点模糊,但似乎表明差异是基于用户 ID,当常规应用程序读取此文件时,模块只会“打印”2 行数据(请注意 Android assigns a unique user ID to each application and runs it as that user in a separate process)。
您没有提供足够的详细信息,但我必须假设当您使用 cat 从 adb 打印它时,您可能没有与尝试阅读时相同的用户 ID 和权限它来自您的应用程序。我没有跟踪确切的实现细节(如果你愿意,the source for this module can be read here),但其他变量可能发挥作用。
In the case of applications that provide network data transfer as a service, such as the download manager, media streaming service, etc, it is possible to attribute the ownership of the network data transfer to the UID of the requesting application using the
TrafficStats.setThreadStatsUid()function call. The caller must hold theandroid.permission.MODIFY_NETWORK_ACCOUNTINGpermission to re-assign the ownership of the network traffic.
因此进程/应用程序可以使用 TrafficStats.setThreadStatsUid() 从该文件中获取更多行,但这需要 MODIFY_NETWORK_ACCOUNTING 权限。
关于java - BufferedReader 不会从文件中读取所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44569701/
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www