备份数据库
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump -uroot -proot plht");
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String b = null;
while((b = bufferedReader.readLine())!=null){
sb.append(b+"\r\n");
}
File file = new File("D:/1.sql");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(sb.toString().getBytes());
inputStream.close();
bufferedReader.close();
fileOutputStream.close();
inputStreamReader.close();
数据库还原
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql -uroot -proot --default-character-set=utf8 plht");
OutputStream outputStream = process.getOutputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:/1.sql")));
StringBuffer sb = new StringBuffer();
String b = null;
while((b = bufferedReader.readLine())!=null){
sb.append(b+"\r\n");
}
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"utf-8");
outputStreamWriter.write(sb.toString());
outputStreamWriter.flush();
outputStreamWriter.close();
bufferedReader.close();
outputStream.close();