我正在尝试从我的系统应用程序读取与系统应用程序关联的 xml 文件。我在java中使用以下代码:
Process p = Runtime.getRuntime().exec("cat /data/data/app_pkg_name/shared_prefs/file.xml");
现在据我了解我可以做到
p.getInputStream()
获取与该进程关联的输入流。如何在 logcat 中获取屏幕上 xml 的实际内容,并使用 system.out.println() 打印它?
当我在 adb shell 中的命令提示符下执行相同的命令(第一个)时,我会在控制台上打印 xml 文件的内容。如何在应用程序内执行此操作?
仅供引用,我的设备已取得 root 权限,并且我已在 list 中使用了必要的权限。
最佳答案
/* You can use the below code to print the output using system.out.println() */
StringBuilder stringBuilder = new StringBuilder();
try {
String contents = "" ;
Process p = Runtime.getRuntime().exec("cat /data/data/com.admarvel.testerofflineappv242/shared_prefs/myPrefs.xml");
InputStream inputStream = p.getInputStream();`enter code here`
BufferedReader in = new BufferedReader(
new InputStreamReader( inputStream ) );
while ( ( contents = in.readLine() ) != null )
{
stringBuilder.append(contents);
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("text" + stringBuilder);
关于java - 如何使用java打印android中进程返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20372575/