import
java.io.BufferedReader;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.io.OutputStream;
import
java.io.OutputStreamWriter;
public
class
Test {
public
static
void
main(String[] args)
throws
IOException{
backup(
"d:\d.sql"
);
recover(
"d:\d.sql"
);
}
public
static
void
backup(String path)
throws
IOException{
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(
"mysqldump -u root -p123456 family"
);
InputStream inputStream = process.getInputStream();
InputStreamReader reader =
new
InputStreamReader(inputStream);
BufferedReader br =
new
BufferedReader(reader);
String s =
null
;
StringBuffer sb =
new
StringBuffer();
while
((s = br.readLine()) !=
null
){
sb.append(s+
"
"
);
}
s = sb.toString();
System.out.println(s);
File file =
new
File(path);
file.getParentFile().mkdirs();
FileOutputStream fileOutputStream =
new
FileOutputStream(file);
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
br.close();
reader.close();
inputStream.close();
}
public
static
void
recover(String path)
throws
IOException{
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(
"mysql -u root -p123456 --default-character-set=utf8 family"
);
OutputStream outputStream = process.getOutputStream();
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(
new
FileInputStream(path)));
String str =
null
;
StringBuffer sb =
new
StringBuffer();
while
((str = br.readLine()) !=
null
){
sb.append(str+
"
"
);
}
str = sb.toString();
System.out.println(str);
OutputStreamWriter writer =
new
OutputStreamWriter(outputStream,
"utf-8"
);
writer.write(str);
writer.flush();
outputStream.close();
br.close();
writer.close();
}
}