首页 > 其他分享 >othertools

othertools

时间:2023-03-31 16:45:32浏览次数:38  
标签:java othertools cmd return import new String

package com.example.testtools;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class otherTool {
    //时间戳转换
    public static String conversionTime(String timeStamp) {
        //yyyy-MM-dd HH:mm:ss 转换的时间格式  可以自定义
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换
        String time = sdf.format(new Date(Long.parseLong(timeStamp)));
        return time;
    }
    
    //字符串匹配计数
    public static int getCount(String source, String sub) {
        int count = 0;
        int length = source.length() - sub.length();
        for (int i = 0; i < length; i++) {
            String sourceBak = source.substring(i, i + sub.length());
            int index = sourceBak.indexOf(sub);
            if (index != -1) {
                count++;
            }
        }
        return count;
    }
    
    //执行shell命令返回
    public static StringBuffer shellExec(String cmd) {
        Runtime mRuntime = Runtime.getRuntime(); //执行命令的方法
        try {
            //Process中封装了返回的结果和执行错误的结果
            Process mProcess = mRuntime.exec(cmd); //加入参数
            //使用BufferReader缓冲各个字符,实现高效读取
            //InputStreamReader将执行命令后得到的字节流数据转化为字符流
            //mProcess.getInputStream()获取命令执行后的的字节流结果
            BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));

            //实例化一个字符缓冲区
            StringBuffer mRespBuff = new StringBuffer();
            //实例化并初始化一个大小为1024的字符缓冲区,char类型
            char[] buff = new char[1024];
            int ch = 0;
            //read()方法读取内容到buff缓冲区中,大小为buff的大小,返回一个整型值,即内容的长度
            //如果长度不为null
            while ((ch = mReader.read(buff)) != -1) {
                //就将缓冲区buff的内容填进字符缓冲区
                mRespBuff.append(buff, 0, ch);
            }
            //结束缓冲
            mReader.close();
            //弹出结果
            Log.d("shell", "执行命令: " + cmd + "执行成功");
            return mRespBuff;

        } catch (IOException e) {
            // 异常处理
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 执行一个shell命令,并返回字符串值
     *
     * @param cmd
     * 命令名称&参数组成的数组(例如:{"/system/bin/cat", "/proc/version"})
     * @param workdirectory
     * 命令执行路径(例如:"system/bin/")
     * @return 执行结果组成的字符串
     * @throws IOException
     */
    //执行shell命令返回
    public static synchronized String run(String[] cmd, String workdirectory)
            throws IOException {
        StringBuffer result = new StringBuffer();
        try {
// 创建操作系统进程(也可以由Runtime.exec()启动)
// Runtime runtime = Runtime.getRuntime();
// Process proc = runtime.exec(cmd);
// InputStream inputstream = proc.getInputStream();
            ProcessBuilder builder = new ProcessBuilder(cmd);
            InputStream in = null;
// 设置一个路径(绝对路径了就不一定需要)
            if (workdirectory != null) {
// 设置工作目录(同上)
                builder.directory(new File(workdirectory));
// 合并标准错误和标准输出
                builder.redirectErrorStream(true);
// 启动一个新进程
                Process process = builder.start();
// 读取进程标准输出流
                in = process.getInputStream();
                byte[] re = new byte[1024];
                while (in.read(re) != -1) {
                    result = result.append(new String(re));
                }
            }
// 关闭输入流
            if (in != null) {
                in.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result.toString();
    }
}

 

标签:java,othertools,cmd,return,import,new,String
From: https://www.cnblogs.com/cwxkurenoki/p/17276684.html

相关文章