⽤Java写脚本,常⽤的⼀些⽅法⽤Java写脚本,常⽤的⼀些⽅法
平时⽤的⼀些⼩⽅法,总结之
1.运⾏⼀个可执⾏程序
⽐如,你如果想运⾏如下命令
C://test// -f params1 -M params2
try {
ProcessBuilder pb = new ProcessBuilder("C://test//","-f","params1","-M","params2");
Process process = pb.start();
InputStream inputStream = InputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
while ((line = adLine()) != null) {
System.out.println(INFO + line);
}
int exit = process.waitFor();
if (exit == 0) {
System.out.println("");
} else {
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
}
注意:
1.调⽤ProcessBuilder的start()⽅法,开始执⾏命令。
2.通过InputStream,把执⾏命令过程中的⽇志打印出来。
3.通过调⽤process.waitFor(),阻塞当前线程,直到命令执⾏完毕后,获得返回码
2.获取当前Class在运⾏时的路径(亦适⽤于jar)
⽐如,获取TestMain这个类在运⾏时的路径。
URL location = ProtectionDomain().getCodeSource()
.
getLocation();
String path = File();
3.获取系统的环境变量
⽐如,获取系统的JAVA_HOME这个环境变量的值
String path = v("JAVA_HOME");
4.删除⽬录
public static boolean deleteDirectory(File directory) {
if (ists()) {
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.读写⽂件
/
**
* 写⽂件
*
java怎么编写* @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 = adLine()) != null) {
content += line;
}
}
return content;
} catch (Exception e) {
System.out.String());
} finally {
try {
if (bufferedReader != null) {
if (bufferedReader != null) {
bufferedReader.close();
bufferedReader = null;
}
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (Exception e) {
System.out.String());
}
}
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);
}
ByteArray();
} 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;
}
6.TODO
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论