用Java写脚本,常用的一些方法
平时用的一些小方法,总结之
1.运行一个可执行程序
比如,你如果想运行如下命令
C://test//aapt.exe -f params1 -M params2
try {
ProcessBuilder pb = new ProcessBuilder("C://test//aapt.exe","-f","params1","-M","params2");
pb.redirectErrorStream(true);
Process process = pb.start();
InputStream inputStream = process.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
System.out.println(INFO + line);
}
int exit = process.waitFor();
if (exit == 0) {
System.out.println("finished...");
} else {
System.out.println("error...");
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
注意:
1.调用ProcessBuilder的start()方法,开始执行命令。
2.通过process.getInputStream,把执行命令过程中的日志打印出来。
3.通过调用process.waitFor(),阻塞当前线程,直到命令执行完毕后,获得返回码
2.获取当前Class在运行时的路径(亦适用于jar)
比如,获取TestMain这个类在运行时的路径。
URL location = TestMain.class.getProtectionDomain().getCodeSource()
.getLocation();
String path = location.getFile();
3.获取系统的环境变量
比如,获取系统的JAVA_HOME这个环境变量的值
String path = System.getenv("JAVA_HOME");
4.删除目录
public static boolean deleteDirectory(File directory) {
if (directory.exists()) {
File[] files = directory.listFiles();
if (null != files) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
}
}
return (directory.delete());
}
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);
out.write(content);
out.println();
fw.close();
out.close();
}
/**
* 读文件
*
* @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 = bufferedReader.readLine()) != null) {
content += line;
}
}
return content;
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
bufferedReader = null;
}
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
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 = in.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
return output.toByteArray();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static boolean saveObject(Serializable serializable,
String filePath) {
try {
FileOutputStream fout = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(serializable);
oos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}