81,094
社区成员
发帖
与我相关
我的任务
分享
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String savePath = savaPath + "asset_" + format.format(now) + ".sql";
String cmd = "C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump -h "
+ mysqlIp + " -u" + mysqlUser + " -p" + mysqlPassword
+ " --default-character-set=utf8 " + dataBaseName;
Runtime runTime = Runtime.getRuntime();
BufferedReader reader = null;
PrintWriter writer = null;
try {
Process process = runTime.exec(new String[]{"sh", "-c", cmd});
reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
String line = null;
File file = new File(savaPath);
if (!file.exists()) {
file.mkdirs();
}
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath), "UTF-8"));
while ((line = reader.readLine()) != null) {
writer.println(line);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
writer.close();
}