将android shell命令的输出写入文件

时间:2015-03-11 11:19:30

标签: android android-manifest file-handling systrace

我正在尝试将atrace的输出写入sdcard。我在用 getRuntime()。exec()(“atrace gfx> /storage/sdcard0/trace.txt”) 在app.App中签名为系统应用程序,命令从终端正常工作。但是从应用程序运行时没有创建文件。

1 个答案:

答案 0 :(得分:0)

我通过从输入流中读取数据并将其写入文件来解决它

 try {


                        Process p = Runtime.getRuntime().exec("atrace -t 5 gfx");
                        p.waitFor();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                        String line = reader.readLine();
                        File myFile = new File("/storage/sdcard0/trac.txt");
                        FileOutputStream f = null;

                        try {
                            f = new FileOutputStream(myFile);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        PrintWriter pw = new PrintWriter(f);
                        while (line != null) {
                            pw.println(line);

                            //Toast.makeText(getBaseContext(), line, Toast.LENGTH_LONG).show();
                            line = reader.readLine();
                        }
                        pw.flush();
                        pw.close();
                        f.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }