日期:2014-05-16 浏览次数:20609 次
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * java程序调用bat文件,执行oracel数据库备份 * @author PanHongBin */ public class ExpOracleData { /** * @param args */ public static void main( String[] args ){ ExpOracleData expOracleData = new ExpOracleData( ); expOracleData.run( ); } public void run( ){ try{ String cmd = "cmd.exe /c G:/dmp/expOracleData.bat"; Process process = Runtime.getRuntime( ).exec( cmd ); ThreadInputStream inputStream = new ThreadInputStream( process ); ThreadErrorStream errorStream = new ThreadErrorStream( process ); inputStream.start( ); errorStream.start( ); if ( process.waitFor( ) == 0 ){ System.out.println( "结束" ); } else{ System.out.println( "正在导出*****" ); } if ( process.exitValue( ) == 0 ){ System.out.println( "数据备份成功。" ); System.out.println( "添加数据备份信息到数据表" ); // save(); } else{ System.out.println( "数据备份失败!" ); } } catch ( Exception e ){ e.printStackTrace( ); } System.out.println( "退出" ); } } class ThreadInputStream extends Thread { Process process = null; public ThreadInputStream( Process process ){ this.process = process; } public void run( ){ InputStream inputStream = process.getInputStream( ); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) ); try{ String line = null; while ( ( line = bufferedReader.readLine( ) ) != null ){ if ( line != null ){ System.out.println( line ); if ( line.indexOf( "exp" ) != -1 ){ System.out.println( "执行数据备份命令" ); break; } } } System.out.println( "输入结束^^^^^^" ); } catch ( IOException e ){ e.printStackTrace( ); } } } class ThreadErrorStream extends Thread { Process process = null; public ThreadErrorStream( Process process ){ this.process = process; } public void run( ){ InputStream errorStream = process.getErrorStream( ); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( errorStream ) ); try{ String line = null; while ( ( line = bufferedReader.readLine( ) ) != null ){ if ( line != null ){ System.out.println( line ); if ( line.indexOf( "成功终止导出" ) != -1 ){ System.out.println( "成功终止导出-------" ); } if ( line.indexOf( "ORA-01017" ) != -1 ){ System.out.println( "用户名 / 密码 错误 ORA-01017-------" ); process.destroy( ); break; } if ( line.indexOf( "EXP-00028" ) != -1 ){ System.out.println( "文件路径不对:EXP-00028-------" ); process.destroy( ); break; } } } System.out.println( "输出结束 ^^^^^^" ); } catch ( IOException e ){ e.printStackTrace( ); } } }
?