import java.io.*;public class Test
{
    public static void main(String[] args)
    {
        try
        {
              // 这里如果去掉2> errors.txt编译成功,加了重定向 ">" 输出就有问题了
              // 实际使用中我需要的就是一个导出文件的功能,这里使用了一个简单的例子
         String command = "javac d:\\Test.java 2> errors.txt";             System.out.println(command);
            
         Process child = Runtime.getRuntime().exec(command);
         System.out.println(child.getErrorStream());
             
              BufferedReader err =
                        new BufferedReader(
                              new InputStreamReader(child.getErrorStream()));
              while ((currentLine = err.readLine()) != null)
                System.out.println(currentLine);
 
        
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }}
各位给点建议?

解决方案 »

  1.   

    你的生成了errors.txt文件了没?
    写这个的目的不是为了编译成功,而是生成这个txt文件。
      

  2.   

    Runtime.getRuntime().exec(command);
    exec参数不知道怎样设定,有没有大哥解决下这个问题
      

  3.   

    文件导出为什么不用FileWriter类来做呢?
      

  4.   

    楼上没明白我的意思,实际中我使用cmd上运行一个外部程序,然后直接生成一个html文件。
      

  5.   

    我把你的程序修改了一下,现在可以生成文本文件了,看看是不是楼主要的功能...
    import java.io.*;public class Export
    {
        public static void main(String[] args)
        {
            try
            {
                  // 这里如果去掉2> errors.txt编译成功,加了重定向 ">" 输出就有问题了
                  // 实际使用中我需要的就是一个导出文件的功能,这里使用了一个简单的例子
             String command = "javac F:\\JAVA\\Test.java"; 
    String currentLine="";
    int lineNum=0;
                
                //System.out.println("A");
                System.out.println(command);
                //System.out.println("B");
                
             Process child = Runtime.getRuntime().exec(command);
             System.out.println(child.getErrorStream());
                 
             //System.out.println("C");
            
             BufferedReader err = new BufferedReader(new InputStreamReader(child.getErrorStream()));
    PrintWriter out1=new PrintWriter(new BufferedWriter(new FileWriter("F:\\JAVA\\Test2.txt")));
    out1.println(String.valueOf(lineNum));
                  while ((currentLine = err.readLine()) != null){
             System.out.println(currentLine);
             out1.println(String.valueOf(lineNum)+" "+currentLine);
             lineNum++;
                  }  out1.close();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }}
      

  6.   

    你程序的功能是通过运行一个外部进程来编译JAVA类文件,再把出错信息输出到文件吧,用FileWriter不行吗?
      

  7.   

    实际中我使用saxon用xslt转换xml生成出一个html文件。这个也可以用FileWriter?
      

  8.   

    命令行中用到如下
    java -cp e:\\saxonb8\\saxon8.jar net.sf.saxon.Transform d:\\query1.xml d:\\query1.xsl > d:\\result.html
    使用这样的命令
      

  9.   

    可以使用child.getOutputStream(),然后写文件
    I/O这块用的比较少,呵呵,我自己再看看,应该差不多了,多谢