JAVA调用命令行2
package loadMBQL; import java.io.File; import java.io.FilenameFilter; public class LoadMBQL { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String exeName = "E:\\ShenTong\\bin\\oimpexp.exe"; String srcFilePath = "E:/source data/MB_QL/node"; String ip = "localhost"; String dbname = "OSRDB"; String port = "2003"; String username = "SYSDBA"; String password = "szoscar55"; File srcFiles = new File(srcFilePath); String cmdStr = "\" -S SYSDBA -T \"MB_QL\" -Y UTF-8 -B 30 -A 1 -d 1 -H " + ip + " -D " + dbname + " -p " + port + " -U " + username + " -P " + password; FilenameFilter filter = new FilenameFilter() { @Override public boolean accept(File dir, String name) { if(name.toLowerCase().endsWith(".bin")){ return true; }else{ return false; } } }; for (File f : srcFiles.listFiles(filter)) { String cmd = exeName + " -F \"" + f.getAbsolutePath() + cmdStr; System.out.print(f.getAbsolutePath()); long start = System.currentTimeMillis(); cmdExec(cmd); System.out.println("\tusing time: " + (System.currentTimeMillis() - start) / 1000 + "s!"); } } public static void cmdExec(String cmdStr) throws Exception { Process p = Runtime.getRuntime().exec(cmdStr); StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR"); errorGobbler.start(); StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT"); outGobbler.start(); int result = p.waitFor(); if (result != 0) { throw new Exception("exe run failed!"); } } }