用Java写脚本,常用的一些方法

时间:2025-01-25 07:18:10

用Java写脚本,常用的一些方法

平时用的一些小方法,总结之

1.运行一个可执行程序

比如,你如果想运行如下命令

C://test// -f params1 -M params2

try {
        ProcessBuilder pb = new ProcessBuilder("C://test//","-f","params1","-M","params2");
        (true);
        Process process = ();
        InputStream inputStream = ();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
        String line = "";
        while ((line = ()) != null) {
            (INFO + line);
        }
        int exit = ();
        if (exit == 0) {
            ("finished...");
        } else {
            ("error...");
        }
    } catch (Exception e) {
        ();
        (-1);
    }


注意:
1.调用ProcessBuilder的start()方法,开始执行命令。
2.通过,把执行命令过程中的日志打印出来。
3.通过调用(),阻塞当前线程,直到命令执行完毕后,获得返回码

2.获取当前Class在运行时的路径(亦适用于jar)

比如,获取TestMain这个类在运行时的路径。

URL location = ().getCodeSource()
            .getLocation();
String path = ();

3.获取系统的环境变量

比如,获取系统的JAVA_HOME这个环境变量的值

String path = ("JAVA_HOME");

4.删除目录

public static boolean deleteDirectory(File directory) {
    if (()) {
        File[] files = ();
        if (null != files) {
            for (int i = 0; i < ; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
    }
    return (());
}

5.读写文件

/**
 * 写文件
 *
 * @param filePath
 * @param sets
 * @throws IOException
 */
public synchronized void writeFile(String filePath, String content)
        throws IOException {
    FileWriter fw = new FileWriter(filePath);
    PrintWriter out = new PrintWriter(fw);
    (content);
    ();
    ();
    ();
}

/**
 * 读文件
 *
 * @param filename
 * @return
 */
public static String readFile(String filepath) {
    File file = new File(filepath);
    InputStream inputStream = null;
    BufferedReader bufferedReader = null;
    try {
        inputStream = new FileInputStream(file);
        String content = "";
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream));
            String line = "";
            while ((line = ()) != null) {
                content += line;
            }
        }
        return content;
    } catch (Exception e) {
        (());
    } finally {
        try {
            if (bufferedReader != null) {
                ();
                bufferedReader = null;
            }
            if (inputStream != null) {
                ();
                inputStream = null;
            }
        } catch (Exception e) {
            (());
        }
    }

    return null;
}

public static byte[] readByte(final InputStream in) throws IOException {
    ByteArrayOutputStream output = null;
    try {
        if (in == null) {
            return null;
        }
        output = new ByteArrayOutputStream(1024 * 2);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = (buffer)) != -1) {
            (buffer, 0, len);
        }
        return ();
    } finally {
        if (output != null) {
            try {
                ();
            } catch (IOException e) {
                ();
            }
        }
        if (in != null) {
            try {
                ();
            } catch (IOException e) {
                ();
            }
        }
    }
}

public static boolean saveObject(Serializable serializable,
        String filePath) {
    try {
        FileOutputStream fout = new FileOutputStream(filePath);
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        (serializable);
        ();
        return true;
    } catch (Exception e) {
        ();
    }
    return false;
}

相关文章