ITPub博客

首页 > 数据库 > Oracle > 用sqlldr批量导文本数据到oracle的一些经验

用sqlldr批量导文本数据到oracle的一些经验

原创 Oracle 作者:jvche12 时间:2017-05-08 11:34:35 1 删除 编辑

最近工作将3T左右的数据导入oracle,按月分类了文件夹,每个文件夹有当月每天的文本文件,每月大概300G。

第一步:分析原始数据文件的格式,创建表,由于数据量大,按每天进行分区,即使这样,每个分区也有10G左右的大小,oracle建议单个表不超过2G,查询应该会较慢
,另原始文件的第一列不需要
create table T_GPSDATA_WEIXINGDINGWEI
(
  bus_id          VARCHAR2(96) not null,
  line_id         VARCHAR2(96) not null,
  child_line_id   VARCHAR2(96) not null,
  orgz_id         NUMBER,
  lct_status      NUMBER,
  lng             NUMBER(15,6),
  lat             NUMBER(15,6),
  lct_altitude    NUMBER(15,6),
  lct_time        DATE,
  lct_speed       NUMBER(15,2),
  lct_direction   NUMBER(38,2),
  edr_speed       NUMBER(15,2),
  edr_miles       NUMBER(15,2)
PARTITION BY RANGE (LCT_TIME) 
INTERVAL(NUMTODSINTERVAL(1,'DAY'))
STORE IN (GPSDATA_WEIXINGDINGWEI) 
(
  PARTITION LCT_TIME_PART01 VALUES LESS THAN (TO_DATE('2015-05-01','yyyy-mm-dd')) TABLESPACE GPSDATA_WEIXINGDINGWEI
);

第二步:创建控制文件的初步模型,导入过程发现了一些问题,需要调整控制文件
load data
characterset AL32UTF8
infile 'E:\gpsdata\201506\weixingdingwei\卫星定位信息2015060205'
append into table T_GPSDATA_WEIXINGDINGWEI
fields terminated by ','
( terminated,
  bus_id,
  line_id,
  child_line_id,
  orgz_id,
  lct_status,
  lng,
  lat,
  lct_altitude,
  lct_time "to_date(:lct_time,'''yyyy-mm-dd hh24:mi:ss''')",
  lct_speed,
  lct_direction,
  edr_speed,
  edr_miles
)
第三步:实验发现几个问题
    第一:文本类型不是txt格式,这个容易,直接写个bat将所有文件类型转换成txt格式
    第二:打开文本文件发现是UTF-16LE格式,这个亲身试验,导入报错,手动改成utf-8格式,就可以导入,在网上找了个文本格式转换的java源代码,具体参考博文后面的BatchConvertFileEncode.java
    第三:lct_time, bus_id, line_id, child_line_id的有些记录为空(2个双引号括起来,中间没有任何内容),并且原始数据文件每个字段都用双引号括起来了,控制文件修改如下:
    第四:每个文件夹有几百个文件,我不可能手动输入infile的每个具体的文件位置及名称,还有每个文件的名称都包含中文,也会报错,用java解决,因此自己写了个代码,具体 参考博文后面的zip3.java
options(errors=999999999)                     //允许的错误数,根据需要修改
load data

characterset AL32UTF8
infile 'E:\gpsdata\201506\weixingdingwei\ 卫星定位信息2015060205 '

append into table T_GPSDATA_WEIXINGDINGWEI
fields terminated by ','                            //表示每个字段由逗号分隔
OPTIONALLY ENCLOSED BY  '"'               //表示字段由双引号括起来
( terminated filler,                                //加上filler表示不导入该列
  bus_id "nullif(:bus_id,'')",                   //nullif对该字段做判断,如果为''(中间没有字符),则插入null显示在oracle就是空
  line_id "nullif(:line_id,'')",
  child_line_id "nullif(:child_line_id ,'')",
  orgz_id,
  lct_status,
  lng,
  lat,
  lct_altitude,
  lct_time "to_date(nullif(:lct_time,''),'''yyyy-mm-dd hh24:mi:ss''')",
  lct_speed,
  lct_direction,
  edr_speed,
  edr_miles
)
最终通过java代码生成如下的控制文件,(zip3.java生成的不完全是,主要是表的字段不一样,这个手动好改):

点击( 此处)折叠或打开

  1. options (errors=999999999 )
  2. load data
  3. characterset AL32UTF8
  4. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060114.txt'
  5. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060115.txt'
  6. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060116.txt'
  7. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060117.txt'
  8. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060118.txt'
  9. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060119.txt'
  10. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060120.txt'
  11. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060121.txt'
  12. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060122.txt'
  13. infile 'E:\gpsdata\201506\weixingdingwei2\weixingdingwei2015060123.txt'

  1. append into table T_GPSDATA_WEIXINGDINGWEI
  2. fields terminated by ','
  3. OPTIONALLY ENCLOSED BY '"'
  4. ( terminated filler ,
  5.   bus_id "nullif(:bus_id,'')" ,
  6.   line_id "nullif(:line_id,'')" ,
  7.   child_line_id "nullif(:child_line_id ,'')" ,
  8.   orgz_id ,
  9.   lct_status ,
  10.   lng ,
  11.   lat ,
  12.   lct_altitude ,
  13.   lct_time "to_date(nullif(:lct_time,'null'),'''yyyy-mm-dd hh24:mi:ss''')" ,
  14.   lct_speed ,
  15.   lct_direction ,
  16.   edr_speed ,
  17.   edr_miles
  18. )




第四步:在处理好的文件夹下,输入cmd,输入:sqlldr userid=gpsdata/gpsdata@its0 control=000input.ctl rows=100160 readsize=20971520 bindsize=20971520 PARALLEL=TRUE
这里用的是并行的( PARALLEL=TRUE)方式导入,速度较快,具体可参考sqlldr性能优化
http://blog.csdn.net/kangkangwanwan/article/details/51869994
另外详细的sqlldr参数可参考
https://wenku.baidu.com/view/63737c19cc7931b765ce1517.html



------附上2个java代码

BatchConvertFileEncode.java的内容,只需修改路径,文件转换前后的格式,文件类型即可

点击( 此处)折叠或打开

  1. package test ;

  2. import java . io . File ;
  3. import java . io . FileFilter ;
  4. import java . io . FileInputStream ;
  5. import java . io . FileOutputStream ;
  6. import java . io . IOException ;
  7. import java . io . InputStreamReader ;
  8. import java . io . OutputStreamWriter ;
  9. import java . util . ArrayList ;
  10. import java . util . List ;

  11. /**
  12.  * 批量转换文件编码
  13.  *
  14.  * @date 2014-10-23
  15.  *
  16.  * @author xsoftlab.net
  17.  */
  18. public class BatchConvertFileEncode {
  19.      /**
  20.      * 获取文件或文件夹 不存在则创建
  21.      *
  22.      * @param path
  23.      *            文件或文件夹路径
  24.      * @return 已有/新创建的文件
  25.      * @throws IOException
  26.      *             可能产生的异常
  27.      */
  28.      public static File getFile ( String path ) throws IOException {
  29.          File file = new File (path ) ;
  30.          if ( file . isDirectory ( ) ) {
  31.              if ( ! file . exists ( ) )
  32.                  file . mkdirs ( ) ;
  33.          } else {
  34.              // 判断目标文件所在的目录是否存在
  35.              if ( ! file . getParentFile ( ) . exists ( ) )
  36.                  file . getParentFile ( ) . mkdirs ( ) ;
  37.              if ( ! file . exists ( ) )
  38.                  file . createNewFile ( ) ;
  39.          }
  40.          return file ;
  41.      }

  42.      /**
  43.      * 递归查找指定后缀名的文件
  44.      *
  45.      * @param folder
  46.      *            目标文件夹
  47.      * @param suffix
  48.      *            目标后缀名
  49.      * @return 找到的文件集合
  50.      */
  51.      public static List < File > searchFile ( File folder , final String suffix ) {
  52.          List < File > result = new ArrayList < File > ( ) ;
  53.          File [ ] subFolders = folder . listFiles ( new FileFilter ( ) { // 运用内部匿名类获得文件
  54.                     @ Override
  55.                      public boolean accept ( File pathname ) { // 实现FileFilter类的accept方法
  56.                          if (pathname . isDirectory ( )
  57.                                  | | (pathname . isFile ( ) & & pathname . getName ( )
  58.                                          . toLowerCase ( )
  59.                                          . endsWith (suffix . toLowerCase ( ) ) ) ) // 根据文件后缀名过滤
  60.                              return true ;
  61.                          return false ;
  62.                      }
  63.                  } ) ;
  64.          if (subFolders ! = null ) {
  65.              for ( File file : subFolders ) {
  66.                  if ( file . isFile ( ) ) {
  67.                      // 如果是文件则将文件添加到结果列表中
  68.                      result . add ( file ) ;
  69.                  } else {
  70.                      // 如果是文件夹,则递归调用本方法,然后把所有的文件加到结果列表中
  71.                      result . addAll (searchFile ( file , suffix ) ) ;
  72.                  }
  73.              }
  74.          }
  75.          return result ;
  76.      }

  77.      /**
  78.      * 单个文件转换编码
  79.      *
  80.      * @param file
  81.      *            要转换的文件
  82.      * @param tarFile
  83.      *            转换后的文件
  84.      * @param charset
  85.      *            转换前的编码
  86.      * @param tarCharset
  87.      *            转换后的编码
  88.      * @throws IOException
  89.      *             可能出现的异常
  90.      */
  91.      public static void convertFileEncode ( File file , File tarFile ,
  92.              String charset , String tarCharset ) throws IOException {
  93.          InputStreamReader reader = null ;
  94.          OutputStreamWriter writer = null ;
  95.          int length ;
  96.          char [ ] b = new char [3 * 1024 ] ;
  97.          try {
  98.              // 打开文件输出流
  99.              reader = new InputStreamReader ( new FileInputStream ( file ) , charset ) ;
  100.              writer = new OutputStreamWriter ( new FileOutputStream (tarFile ) ,
  101.                     tarCharset ) ;
  102.              while ( ( length = reader . read (b ) ) ! = -1 ) {
  103.                  writer . write (b , 0 , length ) ;
  104.                  writer . flush ( ) ;
  105.              }
  106.          } finally {
  107.              // 关闭文件流
  108.              if ( reader ! = null )
  109.                  reader . close ( ) ;
  110.              if ( writer ! = null )
  111.                  writer . close ( ) ;
  112.          }
  113.      }

  114.      /**
  115.      * 根据文件夹批量转换编码
  116.      *
  117.      * @param folder
  118.      *            要转换的文件夹
  119.      * @param tarFolder
  120.      *            输出文件夹
  121.      * @param charset
  122.      *            转换前的编码
  123.      * @param tarCharset
  124.      *            转换后的编码
  125.      * @param suffix
  126.      *            要转换的文件后缀名
  127.      * @throws IOException
  128.      *             可能出现的异常
  129.      */
  130.      public static void convertFileEncodeByFolder ( String folder ,
  131.              String tarFolder , String charset , String tarCharset , String suffix )
  132.              throws IOException {
  133.          String relTar = null ;
  134.          List < File > result = searchFile ( new File (folder ) , suffix ) ; // 调用方法获得文件数组
  135.          System .out . println ( "找到 " + result . size ( ) + " 个需要转换的文件" ) ;
  136.          // 文件尾部处理
  137.          if ( !folder . endsWith ( "/" ) & & !folder . endsWith ( "\\" ) )
  138.             folder + = File .separator ;
  139.          if ( !tarFolder . endsWith ( "/" ) & & !tarFolder . endsWith ( "\\" ) )
  140.             tarFolder + = File .separator ;
  141.          for ( File file : result ) {
  142.              // 使目标文件夹目录层次与源文件夹对应
  143.             relTar = tarFolder
  144.                      + file . getAbsolutePath ( ) . replace (folder . replace ( "/" , "\\" ) ,
  145.                              "" ) ;
  146.             convertFileEncode ( file , getFile (relTar ) , charset , tarCharset ) ;
  147.          }
  148.          System .out . println ( "转换成功!" ) ;
  149.      }

  150.      public static void main ( String [ ] args ) {
  151.          try {
  152.             convertFileEncodeByFolder ( "E:\\gpsdata\\201506\\weixingdingwei" ,
  153.                      "E:\\gpsdata\\201506\\weixingdingwei2" , "UTF-16LE" ,
  154.                      "UTF-8" , "txt" ) ;
  155.          } catch ( IOException e ) {
  156.             e . printStackTrace ( ) ;
  157.          }
  158.      }
  159. }


zip3.java,其中 zipPath是文件夹目录, ctlfile是控制文件名称, oldstr, newstr意思是需要将文件名称内容为 oldstr替换为newstr,
里面有些函数,比如重命名文件,比如自动创建控制文件(主要是为了不手动输入每个文件的位置名称)等,根据需要运行,注释不需要的功能即可


点击( 此处)折叠或打开

  1. package test ;

  2. import java . io . BufferedInputStream ;
  3. import java . io . BufferedOutputStream ;
  4. import java . io . BufferedWriter ;
  5. import java . io . File ;
  6. import java . io . FileInputStream ;
  7. import java . io . FileNotFoundException ;
  8. import java . io . FileOutputStream ;
  9. import java . io . FileWriter ;
  10. import java . io . FilenameFilter ;
  11. import java . io . IOException ;
  12. import java . util . ArrayList ;
  13. import java . util . zip . ZipEntry ;
  14. import java . util . zip . ZipInputStream ;

  15. public class zip3 {

  16.      public static void main ( String [ ] args ) throws IOException {
  17. //        File dir = new File("F:\\ICDATA\\ic卡数据txt版\\201511\\20151108");
  18. //        String strcmd = "cmd /c start F:\\ICDATA\\ic卡数据txt版\\201511\\20151108\\000.bat";  //调用我们在项目目录下准备好的bat文件,如果不是在项目目录下,则把“你的文件名.bat”改成文件所在路径。
  19. //        run_cmd(strcmd,dir);  //调用上面的run_cmd方法执行操作
  20.         
  21.          String zipPath = "E:\\gpsdata\\201506\\weixingdingwei" ;
  22.          String ctlfile = "000input.ctl" ;
  23.          String oldstr = "卫星定位信息" ;
  24.          String newstr = "weixingdingwei" ;
  25.          ArrayList < String > filelist = getFiles ( new ArrayList < String > ( ) ,zipPath ) ;
  26.         
  27.          for ( String str :filelist ) {
  28.              System .out . println (str ) ;
  29.             
  30.              /*批量重命名文件*/
  31.              renameFile (str ,oldstr ,newstr ) ;
  32.             
  33.              /*自动创建控制文件*/
  34. //            createCtlFile(ctlfile,str);
  35.             
  36.              /*自动拷贝bat文件*/
  37. //            copyFile("F:\\ICDATA\\icdatatxt\\000自动导入.bat",str+"\\000自动导入.bat");
  38.             
  39.              /*自动执行bat文件*/
  40. //            File dir = new File(str);
  41. //            String strcmd = "cmd /c start "+str+"\\000.bat";  //调用我们在项目目录下准备好的bat文件,如果不是在项目目录下,则把“你的文件名.bat”改成文件所在路径。
  42. //            run_cmd(strcmd,dir);  //调用上面的run_cmd方法执行操作
  43. //            try {
  44. //                Thread.sleep(60000);
  45. //            } catch (InterruptedException e1) {
  46. //                e1.printStackTrace();
  47. //            }
  48.             
  49.              /*自动执行数据导入*/
  50. //            File dir = new File(str);
  51. //            String strcmd = "cmd /c start "+str+"\\000自动导入.bat";  //调用我们在项目目录下准备好的bat文件,如果不是在项目目录下,则把“你的文件名.bat”改成文件所在路径。
  52. //            run_cmd(strcmd,dir);  //调用上面的run_cmd方法执行操作
  53. //            try {
  54. //                Thread.sleep(60000);
  55. //                break;
  56. //            } catch (InterruptedException e1) {
  57. //                e1.printStackTrace();
  58. //            }            
  59.             
  60.              /*自动删除000.txt文件*/
  61. //            File f = new File(str+"\\000.txt");  // 输入要删除的文件位置
  62. //            if(f.exists())
  63. //                f.delete();
  64. //            else
  65. //                System.out.println("未找到文件");
  66.                         
  67.          }
  68.         
  69.         
  70.      }
  71.     

  72.      static void renameFile ( String path , String oldstr , String newstr )
  73.      {
  74.          File filename = new File (path ) ;
  75.          File [ ] filelist = filename . listFiles ( ) ;
  76.          for ( File file :filelist ) {
  77.              String oldname = file . getName ( ) ;
  78.              //System.out.println(oldname);
  79.              if (oldname . indexOf ( ".txt" ) ! = -1 ) {
  80.                  if (oldname . indexOf (oldstr ) ! = -1 ) {
  81.                      String newname = oldname . replace (oldstr , newstr ) ;
  82.                      System .out . println (newname ) ;
  83.                      file . renameTo ( new File (path + '\\' +newname ) ) ;
  84.                  }
  85.              } else {
  86.                  System .out . println ( "不是txt类型文件" ) ;
  87.              }
  88.          }
  89.      }

  90.      /*创建一个指内容的ctl文件*/
  91.      static void createCtlFile ( String ctlfile , String filePath ) throws IOException {
  92.          String data1 = "load data\r\n" ;
  93.          String data2 = "characterset AL32UTF8\r\n" ;
  94.          String data3 = "infile '" ;
  95.          String data4 = "\\" ;
  96.          String data5 = "'" ;
  97.          String data6 = "\r\n" ;
  98.          String datamonth = "append into table T_GPSDATA_INSTATION\r\n" ;
  99.          String datatable = "fields terminated by ','\r\n" +
  100. "( terminated filler,\r\n" +
  101. "  bus_id,\r\n" +
  102. "  line_id,\r\n" +
  103. "  child_line_id,\r\n" +
  104. "  orgz_id,\r\n" +
  105. "  lct_status,\r\n" +
  106. "  lng,\r\n" +
  107. "  lat,\r\n" +
  108. "  lct_altitude,\r\n" +
  109. "  lct_time " + "\"" + "to_date(:lct_time,'''yyyy-mm-dd hh24:mi:ss''')" + "\",\r\n" +
  110. "  lct_speed,\r\n" +
  111. "  lct_direction,\r\n" +
  112. "  edr_speed,\r\n" +
  113. "  edr_miles,\r\n" +
  114. "  time_in " + "\"" + "to_date(:time_in,'''yyyy-mm-dd hh24:mi:ss''')" + "\",\r\n" +
  115. "  next_stop_id\r\n" +
  116. ")" ;
  117.          String filenameTemp = filePath + "\\" +ctlfile ; //文件路径+文件名称
  118.          System .out . println (filenameTemp ) ;
  119.          File filename = new File (filePath ) ;
  120.          File [ ] strarr = filename . listFiles ( ) ;
  121.          File file = new File (filenameTemp ) ;
  122.          if ( ! file . exists ( ) ) {
  123.              try {
  124.                  file . createNewFile ( ) ;
  125.              } catch ( IOException e ) {
  126.                  // TODO Auto-generated catch block
  127.                 e . printStackTrace ( ) ;
  128.              }
  129.          }

  130.          byte [ ] byteindata1 = data1 . getBytes ( ) ;
  131.          byte [ ] byteindata2 = data2 . getBytes ( ) ;
  132.          try {
  133.              FileOutputStream output = new FileOutputStream (filenameTemp ) ;
  134.              try {
  135.                  output . write (byteindata1 ) ;
  136.                  output . write (byteindata2 ) ;
  137.                  byte [ ] byteinfilepath = filePath . getBytes ( ) ;
  138.                  byte [ ] byteindata3 = data3 . getBytes ( ) ;
  139.                  byte [ ] byteindata4 = data4 . getBytes ( ) ;
  140.                  byte [ ] byteindata5 = data5 . getBytes ( ) ;
  141.                  byte [ ] byteindata6 = data6 . getBytes ( ) ;
  142.                  byte [ ] byteindatamonth = datamonth . getBytes ( ) ;
  143.                  byte [ ] byteindatatable = datatable . getBytes ( ) ;
  144.                  for ( int i = 0 ; i < strarr . length ; i + + ) {
  145.                      //System.out.println(strarr[i].getName());
  146.                      if (strarr [i ] . getName ( ) . equals ( "000input.ctl" ) ) {
  147.                          continue ;
  148.                      }
  149.                      byte [ ] byteinstrname = strarr [i ] . getName ( ) . getBytes ( ) ;
  150.                      output . write (byteindata3 ) ;
  151.                      output . write (byteinfilepath ) ;
  152.                      output . write (byteindata4 ) ;
  153.                      output . write (byteinstrname ) ;
  154.                      output . write (byteindata5 ) ;
  155.                      output . write (byteindata6 ) ;
  156.                  }
  157.                  output . write (byteindatamonth ) ;
  158.                  output . write (byteindatatable ) ;
  159.                  output . flush ( ) ;
  160.                  output . close ( ) ;
  161.              } catch ( IOException e ) {
  162.                  // TODO Auto-generated catch block
  163.                 e . printStackTrace ( ) ;
  164.              }
  165.          } catch ( FileNotFoundException e ) {
  166.              // TODO Auto-generated catch block
  167.             e . printStackTrace ( ) ;
  168.          }
  169.      }

  170.      /*复制文件到指定目录下*/
  171.      static void copyFile ( String path , String copyPath ) {
  172.          try {
  173.              //path: "f://downloads//kon.jpg"
  174.              FileInputStream input = new FileInputStream (path ) ; //可替换为任何路径何和文件名
  175.              //copyPath: "f://kon.jpg"
  176.              FileOutputStream output = new FileOutputStream (copyPath ) ; //可替换为任何路径何和文件名
  177.              int in =input . read ( ) ;
  178.              while (in!=-1 ) {
  179.              output . write ( in ) ;
  180.              in =input . read ( ) ;
  181.              }
  182.              } catch ( IOException e ) {
  183.              System .out . println (e . toString ( ) ) ;
  184.              }
  185.      }
  186.      /*
  187.      * 通过递归得到某一路径下所有的目录及其文件
  188.      */
  189.      static ArrayList < String > getFiles ( ArrayList < String > filelist , String filePath ) {
  190.      File root = new File (filePath ) ;
  191.      File [ ] files = root . listFiles ( ) ;
  192.      for ( File file :files ) {
  193.      if ( file . isDirectory ( ) ) {
  194.      getFiles (filelist , file . getAbsolutePath ( ) ) ;
  195.     
  196.          } else {
  197.                  int a =0 ;
  198.                  String fileParentPath = file . getParent ( ) ;
  199.      for ( String str :filelist ) {
  200.                  if (str . endsWith (fileParentPath ) ) {
  201.                        a =1 ;
  202.                  }
  203.      }
  204.      if (a = =0 ) {
  205.                    filelist . add ( file . getParent ( ) ) ;
  206.          //System.out.println("显示:"+fileParentPath);
  207.      }
  208.      }
  209.      }
  210.      return filelist ;
  211.      }
  212.      /*
  213.      * 添加问价后缀
  214.      */
  215.      static void getFileNames ( String path ) {
  216.          File file = new File (path ) ;
  217.          if ( file . exists ( ) ) {
  218.              File [ ] files = file . listFiles ( ) ;
  219.              if (files . length = = 0 ) {
  220.                  System .out . println ( "文件夹是空的!" ) ;
  221.                  return ;
  222.              } else {
  223.                  for ( File file2 : files ) {
  224.                      if (file2 . isDirectory ( ) ) {
  225.                         getFileNames (file2 . getAbsolutePath ( ) ) ;
  226.                      } else {
  227.                          file2 . renameTo ( new File (file2 . getParent ( ) + "/" + file2 . getName ( ) + ".txt" ) ) ;
  228.                      }
  229.                  }
  230.              }
  231.          } else {
  232.              System .out . println ( "文件不存在!" ) ;
  233.          }
  234.      }
  235.      /*执行bat文件*/
  236.      /*指定bat执行目录*/
  237.      static void run_cmd ( String strcmd , File dir ) {
  238.          //
  239.          Runtime rt = Runtime . getRuntime ( ) ; //Runtime.getRuntime()返回当前应用程序的Runtime对象
  240.          Process ps = null ; //Process可以控制该子进程的执行或获取该子进程的信息。
  241.          try {
  242.                     ps = rt . exec (strcmd , null ,dir ) ; //该对象的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。
  243.                     ps . waitFor ( ) ; //等待子进程完成再往下执行。
  244.          } catch ( IOException e1 ) {
  245.                     e1 . printStackTrace ( ) ;
  246.          } catch ( InterruptedException e ) {
  247.          // TODO Auto-generated catch block
  248.                     e . printStackTrace ( ) ;
  249.          }

  250.          int i = ps . exitValue ( ) ; //接收执行完毕的返回值
  251.          if (i = = 0 ) {
  252.          System .out . println ( "执行完成." ) ;
  253.          } else {
  254.          System .out . println ( "执行失败." ) ;
  255.          }

  256.                 ps . destroy ( ) ; //销毁子进程
  257.                 ps = null ;
  258.      }
  259.        /*不指定执行目录*/
  260. //    static void run_cmd(String strcmd) {
  261. //        //
  262. //                Runtime rt = Runtime.getRuntime(); //Runtime.getRuntime()返回当前应用程序的Runtime对象
  263. //                Process ps = null;  //Process可以控制该子进程的执行或获取该子进程的信息。
  264. //                try {
  265. //                    ps = rt.exec(strcmd);   //该对象的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。
  266. //                    ps.waitFor();  //等待子进程完成再往下执行。
  267. //                } catch (IOException e1) {
  268. //                    e1.printStackTrace();
  269. //                } catch (InterruptedException e) {
  270. //                    // TODO Auto-generated catch block
  271. //                    e.printStackTrace();
  272. //                }
  273. //
  274. //                int i = ps.exitValue();  //接收执行完毕的返回值
  275. //                if (i == 0) {
  276. //                    System.out.println("执行完成.");
  277. //                } else {
  278. //                    System.out.println("执行失败.");
  279. //                }
  280. //
  281. //                ps.destroy();  //销毁子进程
  282. //                ps = null;
  283. //    }
  284.      /**/
  285. }





来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29399342/viewspace-2138676/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2017-05-03

  • 博文量
    3
  • 访问量
    8578