use Java Writing scripts , Some common methods
Some small methods used in daily life , In conclusion
1. Run an executable program
such as , If you want to run the following command
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); }
be careful :
1. call ProcessBuilder Of start() method , Start executing the command .
2. adopt process.getInputStream, Print out the log during the execution of the command .
3. By calling process.waitFor(), Block current thread , Until the command is executed , Get return code
2. Get current Class Path at runtime ( It also applies to jar)
such as , obtain TestMain The runtime path of this class .
URL location = TestMain.class.getProtectionDomain().getCodeSource()
.getLocation(); String path = location.getFile();
3. Get the environment variables of the system
such as , Get the system's JAVA_HOME The value of this environment variable
String path = System.getenv("JAVA_HOME");
4. Delete directory
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. Read and write files
/** * Writing documents * * @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(); }
/** * read file * * @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
Technology