Get command output string. - Android Android OS

Android examples for Android OS:Process

Description

Get command output string.

Demo Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*from w  w  w.java  2  s .  c o m*/
     * Get command output string.
     */
    public static String getCMDOutputString(String[] args) {
        try {
            ProcessBuilder cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] re = new byte[64];
            int len;
            while ((len = in.read(re)) != -1) {
                sb.append(new String(re, 0, len));
            }
            in.close();
            process.destroy();
            return sb.toString();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Related Tutorials