ant自動打包腳本用到的java工具類代碼,一共三個類,打成jar後放到ant安裝路徑lib文件夾下
第一個
import java.io.File;
import java.io.IOException;
/**
* @author
* @time 2018-5-4 下午2:43:41
* 類說明:替換assest文件夾下參數配置文件
*/
public class ReplaceAssestParams {
public static void main(String[] str){
//接收到ant傳進來的參數值
String key1 = str[0];
String key2 = str[1];
String key3 = str[2];
System.out.println("key1:"+key1+", key2:"+key2+", key3:"+key3);
//拼接pywsdk文件路徑
String pywsdkPath = key3+"/pywsdk.data";
//拿到pywsdk中的字符串
String content = FileUtils.readContent(pywsdkPath);
//先複製一份字符串到TXT,用於編譯完成後改回原來默認的值
String backupsPath = key3+"/backups.txt";
File backupsFile = new File(backupsPath);
if(!backupsFile.exists()){
try {
backupsFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//寫入到TXT中
FileUtils.writeContent(backupsFile, content);
//進行參數替換
content = content.replace(key1, key2);
File pywsdkFile = new File(pywsdkPath);
//寫入到pywsdk文件中
FileUtils.writeContent(pywsdkFile, content);
System.out.println("替換參數結果:"+content);
}
}
第二個
import java.io.File;
/**
* @author
* @time 2018-5-8 下午4:57:46
* 類說明:將備份的內容複製進pywsdk文件中
*/
public class ReplaceBackAssestParams {
public static void main(String[] str){
//接收到ant傳進來的參數值
String key1 = str[0];
System.out.println("key1:"+key1);
//拼接備份TXT文件路徑
String backupsPath = key1+"/backups.txt";
//拿到備份TXT中的字符串
String content = FileUtils.readContent(backupsPath);
//拼接pywsdk文件路徑
String pywsdkPath = key1+"/pywsdk.data";
File pywsdkFile = new File(pywsdkPath);
//寫入到pywsdk文件中
FileUtils.writeContent(pywsdkFile, content);
System.out.println("替換回參數結果:"+content);
FileUtils.deleteFile(backupsPath);
}
}
第三個
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
* @author
* @time 2018-5-8 下午5:05:58
* 類說明:文件操作工具類
*/
public class FileUtils {
/**
* 寫入文件
* @author
* @time 2018-5-8 下午5:23:17
* @param file
* @param content
*/
public static void writeContent(File file, String content) {
try {
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out, "utf-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(content);
bufferedWriter.flush();
bufferedWriter.close();
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 讀取文件內容
* @author
* @time 2018-5-8 下午5:23:29
* @param filePath
* @return
*/
@SuppressWarnings("resource")
public static String readContent(String filePath){
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[4096];
int count;
try {
while ((count = inputStream.read(data, 0, 4096)) != -1)
outStream.write(data, 0, count);
outStream.flush();
outStream.close();
inputStream.close();
return new String(outStream.toByteArray(), "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 刪除文件
* @author
* @time 2018-5-8 下午5:23:39
* @param file
*/
public static void deleteFile(String filePath){
File file = new File(filePath);
if(file != null && file.exists()){
file.delete();
}
}
}