• Java调用Bat


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    public class Test3
    {
    	static final String path = "D:\develop\test.bat";
    
    	public static void main(String[] args)
    			throws IOException, InterruptedException
    	{
    		 printJava();
    		createBat();
    		Process p = null;
    		File f = new File("d:");
    		p = Runtime.getRuntime().exec(new String[] { "cmd", "/c", path },null,f);
    		p.waitFor();
    		System.out.print(p.exitValue());
    		printf(p.getInputStream());
    	}
    
    	static void printf(InputStream out) throws IOException
    	{
    		String line = null;
    		BufferedReader reader = new BufferedReader(
    				new InputStreamReader(out, "gbk"));
    		while ((line = reader.readLine()) != null)
    		{
    			System.out.println(line);
    		}
    	}
    
    	static void createBat() throws IOException
    	{
    		File file = new File(path);
    		if (file.exists())
    		{
    			file.delete();
    		}
    		file.createNewFile();
    		PrintWriter pw = new PrintWriter(file);
    		pw.println("echo start");
    		pw.println("pwd");
    		pw.println("cd %JAVA_HOME%");
    		pw.println("cd ..");
    		pw.println("cd jre");
    		pw.println("pwd");
    		pw.println("ls");
    		pw.println("javac Test.java");
    		pw.println("clear");
    		pw.println("for /L %%i in (0,1,2) do start java Test");
    		pw.flush();
    		pw.close();
    	}
    	
    	static void printJava() throws IOException
    	{
    		String home = System.getProperty("java.home");
    		System.out.print(home);
    		File dir = new File(home);
    		File file = new File(dir,"Test.java");
    		if(file.exists())
    			file.delete();
    		file.createNewFile();
    		PrintWriter pw = new PrintWriter(file);
    		pw.println("public  class Test");
    		pw.println("{");
    			pw.println("public static void main(String[] args)");
    			pw.println("{");
    				 	pw.println("System.out.println("hello world");");
    				 	pw.println("int i = 0;");
    				 	pw.println("while(i++ < 100000) System.out.println("hello world");");
    			 pw.println("}");
    		pw.println("}");
    		pw.flush();
    		pw.close();
    	}
    }
    

      

  • 相关阅读:
    Java-集合类源码List篇(二)
    规范输入
    Java 构造方法
    c语言函数实现交换两个数的值
    2015北京宇信易诚科技面试题
    Java中的访问控制
    2015大连华信校园招聘面试题--堆栈
    2015大连华信面试题OSI七层模型
    2015大连华信面试题二叉树、遍历
    2015大连华信面试题MVC框架
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6812968.html
Copyright © 2020-2023  润新知