// 方式1
String command = "D:\\mysql\\bin\\mysqldump -h127.0.0.1 -uroot -p -ntd -R -dbname";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
InputStreamReader reader = new InputStreamReader(process.getInputStream());
BufferedReader buffReader = new BufferedReader(reader);
String inStr;
String outStr;
StringBuffer buffer = new StringBuffer();
while ((inStr = buffReader.readLine()) != null) {
buffer.append(inStr + "\r\n");
}
outStr = buffer.toString();
OutputStream os = new FileOutputStream("D:\\sql.sql");
OutputStreamWriter writer = new OutputStreamWriter(os);
writer.write(outStr);
writer.flush();
reader.close();
buffReader.close();
os.close();
writer.close();
// 方式2
String command = "cmd /c mysqldump -h127.0.0.1 -uroot -p -ntd -R -dbname";
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command, null,new File("D:\\mysql\\bin"));
InputStreamReader reader = new InputStreamReader(process.getInputStream());
BufferedReader buffReader = new BufferedReader(reader);
String inStr;
String outStr;
StringBuffer buffer = new StringBuffer();
while ((inStr = buffReader.readLine()) != null) {
buffer.append(inStr + "\r\n");
}
outStr = buffer.toString();
OutputStream os = new FileOutputStream("D:\\sql.sql");
OutputStreamWriter writer = new OutputStreamWriter(os);
writer.write(outStr);
writer.flush();
reader.close();
buffReader.close();
os.close();
writer.close();