Java 在Window及Linux下备份MySQL数据库(3)

public void load() {
        try {
            String fPath = "c:\\test.sql";
            Runtime rt = Runtime.getRuntime();
            Process child = rt.exec("mysql -uroot -pjwl shequ");
            OutputStream out = child.getOutputStream();
            // 控制台的输入信息作为输出流
            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);
            // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
            writer.flush();
            out.close();
            br.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsddy.html