java中使用mysqldump 备份数据库: 见代码String hostIP = "127.0.0.1";String userName = "root";String password = "123456";String savePath = "C:/Users/Administrator/Desktop/";String fileName = "SetofBook"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".sql";String databaseName = "sss";File saveFile = new File(savePath);// 如果目录不存在if (!saveFile.exists()) { // 创建文件夹 saveFile.mkdirs();}if(!savePath.endsWith(File.separator)){ savePath = savePath + File.separator;} PrintWriter printWriter = null;BufferedReader bufferedReader = null;try { printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8")); Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName); InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8"); bufferedReader = new BufferedReader(inputStreamReader); String line; while((line = bufferedReader.readLine())!= null){ printWriter.println(line); } printWriter.flush(); //0 表示线程正常终止。 if(process.waitFor() == 0){ return true; }}catch (Exception e) { e.printStackTrace();} finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (printWriter != null) { printWriter.close(); } } catch (IOException e) { e.printStackTrace(); }}
原文地址:https://www.cnblogs.com/youyouxiaosheng-lh/p/11026817.html
时间: 2024-11-01 13:46:28