• DATAX 实现java调用cmd 系统控制台,实现在开发工具中内嵌datax


    二话不说直接给代码

     1 package UTIL;
     2 import java.io.BufferedReader;
     3 import java.io.BufferedWriter;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 import java.io.OutputStream;
     7 import java.io.OutputStreamWriter;
     8 
     9 public class CMD {
    10     Process process;
    11     public void CmdTest(String json) throws IOException {
    12         process=Runtime.getRuntime().exec("cmd");
    13         new Thread(){
    14             public void run() {
    15                 try {
    16                     BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
    17                     bw.write("CHCP 65001");
    18                     bw.newLine();
    19 
    20                     bw.write("e:");
    21                     bw.newLine();
    22 
    23                     bw.write("cd E:\DATAX\datax\bin");
    24                     bw.newLine();//因为读取时是一行行读取的,不加newline无法继续往下读
    25 
    26                     bw.write("python datax.py "+json);
    27                     bw.newLine();
    28 
    29 
    30                     bw.flush();
    31                 } catch (IOException e) {
    32                     // TODO Auto-generated catch block
    33                     e.printStackTrace();
    34                 }
    35             }
    36         }.start();;
    37         new Thread() {
    38             public void run() {
    39                 try {
    40                     BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));
    41                     String cmdout="";
    42                     while ((cmdout=br.readLine())!=null) {
    43                         System.out.println(cmdout);
    44                     }
    45                 } catch (IOException e) {
    46                     // TODO Auto-generated catch block
    47                     e.printStackTrace();
    48                 }
    49             }
    50         }.start();
    51 
    52     }
    53 
    54     public static void main(String[] args) throws IOException {
    55         CMD cmd=new CMD();
    56         String json="E:\DATAX\Workplace\TEXT\csv2csv.json";
    57         cmd.CmdTest(json);
    58 
    59     }
    60 }
  • 相关阅读:
    [译]Java例解Interface
    【笔记】2014-01至2014-02
    【笔记】2013-12
    【笔记】2013-10至2013-11
    【笔记】2013-09
    【笔记】2013-08
    【笔记】2013-07
    生活工作中好用的快捷键和小工具(更新)
    Android Studio更新Gradle问题
    gradle构建spring5源码
  • 原文地址:https://www.cnblogs.com/smartisn/p/12390964.html
Copyright © 2020-2023  润新知