package com.offcn.TestUnti;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

public class cmdUtil {
	
    public static String run(String cmdString) {
        Runtime runtime = Runtime.getRuntime();
        StringBuffer b=null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(runtime.exec(cmdString).getInputStream(),"GBK"));
            //StringBuffer b = new StringBuffer();
            String line=null;
            b=new StringBuffer();
            while ((line=br.readLine())!=null) {
                b.append(line+"\n");
            }
            System.out.println(b.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b.toString();

    }

    public static void main(String[] args) throws IOException {

    	Properties properties = System.getProperties();
    	System.setProperty("path", "C:\\Program Files (x86)\\PICT\\;");
		Iterator it =  properties.entrySet().iterator();
		while(it.hasNext())
		{
			Entry entry = (Entry)it.next();
			System.out.print(entry.getKey()+"=");
			System.out.println(entry.getValue());
		}
//    	String batName = "C:/Users/ibm/git/tiku_app/tiku_App/src/resources/abc.bat";
//		Runtime rt = Runtime.getRuntime();
//		Process ps = null;
//		try {
////			ps = rt.exec("cmd.exe /C start /b " + batName);
//			ps = rt.exec("cmd.exe /c " + "copy C:/Users/ibm/git/tiku_app/tiku_App/src/resources/abc.bat C:/Users/ibm/git/tiku_app/tiku_App/src/resources/abc.bat111");
//			System.out.println(ps.waitFor());
////			FileInputStream fis = null;
////			fis=(FileInputStream) ps.getErrorStream();
////
////				byte[] buf = new byte[1024];
////				int len = 0;
////				while((len=fis.read(buf))!=-1)
////				{
////					System.out.println(new String(buf,0,len));
////				}
////
////				fis.close();
//		} catch (IOException e1) {
//		e1.printStackTrace();
//		} catch (InterruptedException e) {
//		    	e.printStackTrace();
//		    	}
//		int i = ps.exitValue();
//		if (i == 0) {
//			System.out.println("执行完成.");
//		} else {
//			System.out.println("执行失败.");
//    	}
        
//        try {
//        	File directory = new File(".");
//            String sourceFile;
//            String sourceFile1;
//    		sourceFile = directory.getCanonicalPath() +File.separator+"src"+File.separator+"resources"+File.separator+"case.txt";
//    		sourceFile1 = directory.getCanonicalPath() +File.separator+"src"+File.separator+"resources"+File.separator;
//        	String cmd = "pict "+sourceFile+">"+sourceFile1+"caseOut.txt";
//            String cmd2 = "cmd /c  " + "\"\" " + cmd ;
//            String cmd3 = "cmd.exe /c " + "copy C:/Users/ibm/git/tiku_app/tiku_App/src/resources/abc.bat C:/Users/ibm/git/tiku_app/tiku_App/src/resources/abc.bat111";
//            System.out.println(cmd3);
//        	run(cmd3);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    }

    public static void processBuilderCommand() throws Exception {
        
    	File directory = new File(".");
        String sourceFile;
        String sourceFile1;
		sourceFile = directory.getCanonicalPath() +File.separator+"src"+File.separator+"resources"+File.separator+"case.txt";
		sourceFile1 = directory.getCanonicalPath() +File.separator+"src"+File.separator+"resources"+File.separator;
//        List<String> commands = new ArrayList<>();
//        commands.add("cmd.exe");
//        commands.add("cd C:\\Program Files (x86)\\PICT");
//        commands.add("pict "+sourceFile+">"+sourceFile+"caseOut.txt");
//        commands.add("\r\n");
//        ProcessBuilder pb =new ProcessBuilder(commands);
        //可以修改进程环境变量
//        pb.environment().put("DAXIN_HOME", "/home/daxin");
//        System.out.println(pb.directory());
        String cmd = "pict "+sourceFile+">"+sourceFile1+"caseOut.txt";
        String cmd2 = "cmd /c start " + "\"\" " + cmd ;
        System.out.println(cmd2);

//	    String  []cmd1 = {"cd" , " C:\\Program Files (x86)\\PICT ","pict","case.txt>caseOut.txt"};
        Process process = Runtime.getRuntime().exec(cmd2); 
        process.waitFor();
        process.exitValue();
//        System.out.println(pb.environment());
//         
//        System.out.println(status);
//        InputStream in = process.getInputStream();
//         
//        BufferedReader br = new BufferedReader(new InputStreamReader(in));
//        String line = br.readLine();
//        while(line!=null) {
//            System.out.println(line);
//            line = br.readLine();
//        }
//         
    }
}
