Android shell 查詢目錄文件列表和大小等信息(原創)

1、FileUtils工具類

 /**
     * 獲取目錄下所有文件及文件夾大小等信息,單位kb
     *
     * @param path
     * @return
     */
    public static String getPathMsgFromShell(String path) {
        String command = "du -k  " + path + " |sort -n";
        ShellUtils.CommandResult commandResult = ShellUtils.execCommand(command, false, true);
        return commandResult.successMsg;
    }

    /**
     * 通過shell命令獲取文件目錄總大小,注意:成功返回爲 14M 目錄 String結果,失敗null或者空數據
     *
     * @param path
     * @return
     */
    public static String getPathSizeFormShell(String path) {
        String command = "du -sh  " + path;
        ShellUtils.CommandResult commandResult = ShellUtils.execCommand(command, false, true);
        return "path size:" + commandResult.successMsg;
    }

2、ShellUtils工具類

public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
        int result = -1;
        if (commands == null || commands.length == 0) {
            return new CommandResult(result, null, null);
        }

        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;

        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
            os = new DataOutputStream(process.getOutputStream());
            for (String command : commands) {
                if (command == null) {
                    continue;
                }

                // do not use os.writeBytes(command), avoid chinese charset error
                os.write(command.getBytes());
                os.writeBytes(COMMAND_LINE_END);
                os.flush();
            }
            os.writeBytes(COMMAND_EXIT);
            os.flush();

            result = process.waitFor();
            // get command result
            if (isNeedResultMsg) {
                successMsg = new StringBuilder();
                errorMsg = new StringBuilder();
                successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
                errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                String s;
                while ((s = successResult.readLine()) != null) {
                    successMsg.append(s);
                }
                while ((s = errorResult.readLine()) != null) {
                    errorMsg.append(s);
                }
            }
        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (process != null) {
                process.destroy();
            }
        }
        return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
                : errorMsg.toString());
    }

不清楚留言,有時間會解答。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章