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()) ;
}
}
}
??????????
实际上,我希望我的应用程序能够在文件中显示编译状态(而不是在命令提示符中显示),并且在创建 文件后,我希望以编程方式打开该文件,以便我可以查看编译状态.... – mogli 2009-06-17 01:09:35
我已经试过你的代码,但即使这不工作.... 还有更多的问题... 如果我wana显示你,我试过什么代码, 在哪里发表回复,因为这个评论框不支持代码格式... :-) – mogli 2009-06-17 01:19:17
你可以编辑你的原始的帖子与新的代码示例 – akf 2009-06-17 01:57:46