日期:2014-05-17 浏览次数:20722 次
package com.hancai.base.admin.action; 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{ recover("d:\\fjsdljfls.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+"\r\n"); } 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 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+"\r\n"); } 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(); } }
Exception in thread "main" java.io.IOException: 管道已结束。 at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:260) at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202) at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:263) at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:106) at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:116) at java.io.OutputStreamWriter.write(OutputStreamWriter.java:203) at java.io.Writer.write(Writer.java:140) at com.hancai.base.admin.action.Test.recover(Test.java:64) at com.hancai.base.admin.action.Test.main(Test.java:26)