2009-06-17 76 views
1
import java.io.*; 

public class Demo{ 

    public static void main(String[] args){ 
     File f = new File("abc.txt") ; 

     try{ 
      System.setOut(new PrintStream(new FileOutputStream(f))) ; 
     } 
     catch(FileNotFoundException fnfe){ 
      System.out.println(fnfe.getMessage()) ; 
     } 

     System.out.println("Hello\n") ; 

     try{ 
      //throwing exception, 
      //is there any method to close the f File, 
      //before we try to open the file referred by f. 
      Process p = Runtime.getRuntime().exec(f.getPath()) ; 
     } 
     catch(IOException io){ 
      System.out.println(io.getMessage()) ; 
     } 
    } 

} 

和的abc.txt的执行演示后的内容是: -执行动态创建的文件

你好

不能运行程序 “的abc.txt”:CreateProcess的误差= 32,所述的因为它正被另一个进程使用

如何避免异常.....

进程无法访问该文件这里

尽可能多的人的建议,我曾尝试下面的代码, 但可悲的是,即使这样也扔excption .... :-(

import java.io.*; 

class Demo{ 

    public static void main(String[] args){ 
     File f = new File("abc.txt") ; 

     FileOutputStream fos = null ; 
     try{ 
      fos = new FileOutputStream(f) ; 
     } 
     catch(FileNotFoundException fnfe){ 
      System.out.println(fnfe.getMessage()) ; 
     } 

     PrintStream ps = new PrintStream(fos) ; 
     ps.println("Hello") ; 

     try{ 
      fos.close() ; 

      //throwing exception again 
      Process p = Runtime.getRuntime().exec(f.getAbsolutePath()) ; 
     } 
     catch(IOException io){ 
      System.out.println(io.getMessage()) ; 
     } 
    } 
} 

??????????

回答

2

我假设调用Runtime.getRuntime()。exec(f.getPath())的原因;是在文本编辑器中打开abc.txt文件。提供完整的命令以打开文本编辑器以及文件路径会更好。我试图用notepad.exe(窗口),它的工作。

import java.io.*; 

public class Demo{ 

    public static void main(String[] args){ 
     File f = new File("abc.txt") ; 

     try{ 
       System.setOut(new PrintStream(new FileOutputStream(f))) ; 
     } 
     catch(FileNotFoundException fnfe){ 
       System.out.println(fnfe.getMessage()) ; 
     } 

     System.out.println("Hello\n") ; 

     try{ 
       Process p = Runtime.getRuntime().exec("notepad.exe " + f.getPath()) ; 
     } 
     catch(IOException io){ 
       System.out.println(io.getMessage()) ; 
     } 
    } 

} 

以下代码动态生成Java代码,并使用的javac编译它

import java.io.*; 

public class Demo{ 

    public static void main(String[] args){ 

     File f = new File("Abc.java") ; 
     PrintWriter writer = null; 
     BufferedReader reader = null; 
     InputStream pStream = null; 

     try{ 
       // Open File Stream and write code 
       writer = new PrintWriter(new FileOutputStream(f)); 
       String javaCode = "public class Abc { \r\n" + 
            "public static void main(String[] args) {\r\n" + 
            "System.out.println(\"Hello World!\");\r\n" + 
            "}\r\n" + 
            "}\r\n"; 

       writer.println(javaCode) ; 
       writer.close(); 

       // Run Javac to compile the code 
       Process p = Runtime.getRuntime().exec("javac " + f.getPath()) ; 
       p.waitFor(); 

       // status = 0 => Process executed without errors 
       //  = 1 => Process executed with errors 
       int status = p.exitValue(); 
       if(status == 0) 
       { 
        pStream = p.getInputStream(); 
       } 
       else 
       { 
        pStream = p.getErrorStream(); 
       } 

       // Display the output from the process 
       reader = new BufferedReader(new InputStreamReader(pStream)); 
       String ln = null; 
       while((ln = reader.readLine()) != null) 
       { 
        System.out.println(ln); 
       } 
     } 
     catch(Exception ex){ 
       System.out.println(ex.getMessage()) ; 
     } 
     finally{ 
      try{ 
       if(writer != null){writer.close();} 
       if(pStream != null){pStream.close();} 
       if(reader != null){reader.close();} 
      } 
      catch(Exception ex){ 
       System.out.println(ex.getMessage()) ; 
      } 
     } 
    } 
} 
0

在尝试执行它之前关闭FileOutputStream(或PrintStream)。

1

执行前关闭文件(不重定向的System.out):

f = new File("abc.txt"); 
FileOutputStream fos = new FileOutputStream(f); 

// You would likely use fos.write instead, but here we go 
PrintStream ps = new PrintStream(fos); 
ps.println("Hello\n"); 

fos.close(); 

Process p = Runtime.getRuntime().exec(f.getPath()); 
+0

实际上,我希望我的应用程序能够在文件中显示编译状态(而不是在命令提示符中显示),并且在创建 文件后,我希望以编程方式打开该文件,以便我可以查看编译状态.... – mogli 2009-06-17 01:09:35

+0

我已经试过你的代码,但即使这不工作.... 还有更多的问题... 如果我wana显示你,我试过什么代码, 在哪里发表回复,因为这个评论框不支持代码格式... :-) – mogli 2009-06-17 01:19:17

+0

你可以编辑你的原始的帖子与新的代码示例 – akf 2009-06-17 01:57:46

0
  1. 你的程序创建一个文本文件,而不是一个可执行文件。可执行文件是操作系统知道如何执行的二进制文件(或者在某些情况下是具有特殊头文件的脚本)。我不确定您在执行内容为“Hello \ n”的文本文件时会发生什么。

  2. 您正在将stdout(标准输出)重定向到您的文件。稍后,您将异常堆栈跟踪打印到stdout,这就是跟踪出现在文件中的原因。您可能更有意义的是直接写入文件,而不是重定向stdout。

+0

但跟踪发生在catch块中,并且我正在使用try块中的exec来打开该文件,该块在执行前明确执行.... – mogli 2009-06-17 01:47:59

0

假设你在Windows上运行,而不是

Process p = Runtime.getRuntime().exec(f.getPath());

你可以使用

Process p = Runtime.getRuntime().exec("start " + f.getPath());

应选择与.txt文件关联的任何应用程序。