Java動態生成bat檔案備份資料庫
阿新 • • 發佈:2019-01-08
String t = Thread.currentThread().getContextClassLoader().getResource("").getPath();獲取釋出執行路徑
//解決路徑中空格的問題
t=URLDecoder.decode(t,"utf-8");
int num = t.indexOf("test");查詢此專案跟目錄位置
String path = t.substring(1, num).replace('/', '\\')+ "test\\static\\bat\\1.bat";寫bat檔案路徑
String data ="\"C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysqldump\" -u \"root\" -p\"root\" -P\"3306\" \"test\"> \""+ mysqlPathString + "\\" + fileNameString + ".sql\"";
//bat內容
data.replace("\'", "\"");
File txt = new File(path);
txt.createNewFile();建立檔案
byte bytes[] = new byte[512]; bytes = data.getBytes();寫檔案
int b = data.length();
FileOutputStream fos = new FileOutputStream(txt);
fos.write(bytes, 0, b);
fos.close();
runtime.exec(path);