用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;
}

6.TODO

用Java写脚本,常用的一些方法相关推荐

  1. loadrunner写脚本常用C函数

    loadrunner写脚本常用C函数 strcat的串连两个字串. strchr返回指向第一次出现的字符串中的字符. STRCMP比较两个字符串来确定的字母顺序. STRCPY一个字符串复制到另一个地 ...

  2. List精讲(Java版)·算法常用集合处理方法

    List精讲(Java版)·算法常用集合处理方法 请仔细查阅每一个注释: import java.util.ArrayList; import java.util.Collections; impor ...

  3. Java String API 常用的String方法详解

    标题 String类的特性 Java中String类的构造方法 String类的 intern() 注意还跟jdk有关 如何保证变量S指向的是字符串常量池中的数据呢? 关于String中 new St ...

  4. 帮我用Java写一个生成流水号的方法

    我可以给你提供一个使用Java来生成流水号的简单方法: 使用java.util.UUID类来生成一个唯一的字符串: 使用java.time.LocalDateTime类来获取当前的日期和时间: 将上述 ...

  5. java easing_jQuery中常用的函数方法总结

    jQuery中为我们提供了很多有用的方法和属性,自己总结的一些常用的函数,方法.个人认为在www.2JavaScript jQuery中为我们提供了很多有用的方法和属性,自己总结的一些常用的函数,方法 ...

  6. 如何使用Java写“脚本”(单个Java文件如何像脚本一样使用运行)

    Java 支持在类 Unix 系统上将单个 Java 源代码文件像脚本一样运行,但是和真正的脚本不一样,还是需要编译的.也就是说需要在 Linux.macOS.WSL 下运行,Windows Powe ...

  7. java 写文件 flush_Java FileOutputStream flush()方法

    Java FileOutputStream flush()方法 java.io.FilterOutputStream.flush() 用于刷新此输出流,并强制将所有缓冲的输出字节写出到该流中. 1 语 ...

  8. java 写文件的三种方法比较

    原文:http://www.cnblogs.com/yezhenhan/archive/2012/09/10/2678690.html import java.io.File; import java ...

  9. 用java写一个日期相减方法

    你好,这是一个用于计算两个日期之间的天数差的 Java 方法: import java.time.LocalDate; import java.time.temporal.ChronoUnit;pub ...

  10. java开发中常用到小方法

    ArrayList转数组 /** 获取优惠券全部策略ID String[]*/private String[] getStrategyIDArrayCache() throws Exception { ...

最新文章

  1. 01背包和完全背包问题
  2. 聊聊那些令人惋惜的车模
  3. 新书发布《每天5分钟玩转Docker容器技术》
  4. oracle11g ora 12545,sqlplus本地登录报错ORA-12545
  5. java 并发 面试题_阿里面试官总结10个经典技术题:Java多线程与并发面试题
  6. SharePoint2013 Excel导出好的代码
  7. 为何加入了AddType就无法启动Apache
  8. 使用Nagios打造专业的业务状态监控
  9. 汇编语言编译器masm_计算机汇编语言和指令操作
  10. android 隐藏所有 fragment,Android 隐藏Fragment
  11. Python字符串的替换
  12. Bailian4123 马走日【DFS】
  13. 地方舆情监测软件排名怎么评估的参考方法详解
  14. WAS中间件服务器简介
  15. 深入Android应用开发_核心技术解析与最佳实践
  16. 在Mac上使用idea搭建flink java开发环境
  17. 极智AI | 目标检测实现分享二:听说克莱今天复出了?详解 YOLOv2 算法与克莱检测
  18. 京东股权众筹投后总结和反思
  19. 2019, XII Samara Regional Intercollegiate Programming Contest 解题报告
  20. 2015-4-11更新的pdf

热门文章

  1. 利用Python爬取3万多条上海二手房信息,我得出的结论是?
  2. java程序设计大赛acm_转载(ACM国际大学生程序设计大赛)
  3. XLua系列讲解_Helloworld
  4. python-微信公众个性二维码生成-生成自己名片二维码-链接二维码【超酷】
  5. 计算机毕业设计Java大学生旅游拼团网站(源码+系统+mysql数据库+lw文档)
  6. 报错---qt.qpa.plugin
  7. 【STM32】ADC库函数、一般步骤详解(实例:内部温度传感器实验)
  8. 西瓜数据集3.0 python_决策树对西瓜数据集2.0二分类
  9. 联想笔记本BIOS设置中文详解
  10. 论文写作参考文献 期刊标准缩写