Package com.backup;
Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
public class Test {
/**
* @param args
*/
Public static void Main (string[] args) {
/*
* Backup and import is a process of reciprocal inversion.
* Backup: The program calls the MySQL backup command, reads the console input stream information, writes the. sql file;
* Import: The program calls the MySQL import command to write the information read from the. sql file to the console's output stream
* Note: This means that the ">" and "<" are not available at this time
*/
//backup ();
//load ();
}
/**
* Backup verifies whether a SQL file can be used as an import file using a method of judging: Put the SQL file separately in Notepad and ultra
* Edit Open, if you see normal Chinese is not garbled, it can be used to do the import of the source file (regardless of the encoding format of the SQL file, regardless of the encoding format of the DB)
*/
public static void Backup () {
try {
Runtime RT = Runtime.getruntime ();
Call the MySQL cmd:
Process child = RT
. exec ("Mysqldump-u root--set-charset=utf8 pj");//Set export encoding to UTF8. This must be UTF8.
Writes the console output information from the process execution to the. sql file, which generates the backup file. Note: If you do not read the console information, it will cause the process to become blocked from running
InputStream in = Child.getinputstream ();//console output information as input stream
InputStreamReader xx = new InputStreamReader (in, "UTF8");//Set output stream encoding to UTF8. This must be UTF8, otherwise the read from the stream is garbled
String inStr;
StringBuffer sb = new StringBuffer ("");
String outstr;
Combining console output information strings
BufferedReader br = new BufferedReader (XX);
while ((inStr = Br.readline ()) = null) {
Sb.append (inStr + "\ r \ n");
}
outstr = Sb.tostring ();
SQL destination file to use for import:
FileOutputStream fout = new FileOutputStream (
"E:/bjse22.sql");
OutputStreamWriter writer = new OutputStreamWriter (fout, "UTF8");
Writer.write (OUTSTR);
Note: If the file is written in buffer mode, it will result in Chinese garbled, and the flush () method can avoid
Writer.flush ();
Don't forget to close the input and output stream
In.close ();
Xx.close ();
Br.close ();
Writer.close ();
Fout.close ();
SYSTEM.OUT.PRINTLN ("/* Output ok! */");
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Import
*
*/
public static void Load () {
try {
String Fpath = "E:/bjse22.sql";
Runtime RT = Runtime.getruntime ();
Call the MySQL cmd:
Process child = Rt.exec ("Mysql-u root PJ");
OutputStream out = Child.getoutputstream ();//console input information as the output stream
String inStr;
StringBuffer sb = new StringBuffer ("");
String outstr;
BufferedReader br = new BufferedReader (New InputStreamReader (
New FileInputStream (Fpath), "UTF8");
while ((inStr = Br.readline ()) = null) {
Sb.append (inStr + "\ r \ n");
}
outstr = Sb.tostring ();
OutputStreamWriter writer = new OutputStreamWriter (out, "UTF8");
Writer.write (OUTSTR);
Note: If the file is written in buffer mode, it will result in Chinese garbled, and the flush () method can avoid
Writer.flush ();
Don't forget to close the input and output stream
Out.close ();
Br.close ();
Writer.close ();
SYSTEM.OUT.PRINTLN ("/* Load ok! */");
} catch (Exception e) {
E.printstacktrace ();
}
}
}
This article transferred from: http://www.blogjava.net/zhaosoft/archive/2008/10/30/237514.html
Java implementation MySQL data import and export