2 回答
TA贡献1824条经验 获得超6个赞
p.getErrorStream()
您可以使用and读取进程的输出,p.getInputStream()
这应该在新线程中完成,然后您可以将输出写入日志文件或控制台。
TA贡献1836条经验 获得超4个赞
这是我尝试过并且工作正常的方法,
有关 mysqldump 命令的更多信息:https ://stackoverflow.com/a/13484728/2987755
您可以提供要在其中创建备份文件的任何路径db_backup.sql。
Process rt = Runtime.getRuntime().exec("mysqldump -P 3306 -h 127.0.0.1 -u root test");
int exitCode = rt.waitFor();
System.out.println("Process exited with : " + exitCode);
BufferedReader in = new BufferedReader(new InputStreamReader(rt.getInputStream()));
BufferedReader err = new BufferedReader(new InputStreamReader(rt.getErrorStream()));
System.out.println("Backup file output:");
String line;
BufferedReader reader;
if (exitCode != 0) {
reader = err;
} else {
reader = in;
}
File file = new File("db_backup.sql");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
while ((line = reader.readLine()) != null) {
System.out.println(line);
bufferedWriter.write(line);
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedWriter.close();
添加回答
举报