Java tutorial
//package com.java2s; /* * Copyright (C) 2012 www.amsoft.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.io.InputStream; public class Main { public static String runCommandTopN1() { String result = null; try { String[] args = { "/system/bin/top", "-n", "1" }; result = runCommand(args, "/system/bin/"); } catch (Exception e) { e.printStackTrace(); } return result; } public static String runCommand(String[] command, String workdirectory) { String result = ""; //AbLogUtil.d(AbAppUtil.class, "#"+command); try { ProcessBuilder builder = new ProcessBuilder(command); // set working directory if (workdirectory != null) { builder.directory(new File(workdirectory)); } builder.redirectErrorStream(true); Process process = builder.start(); InputStream in = process.getInputStream(); byte[] buffer = new byte[1024]; while (in.read(buffer) != -1) { String str = new String(buffer); result = result + str; } in.close(); } catch (Exception e) { e.printStackTrace(); } return result; } }