gpt4 book ai didi

java - 执行动态创建的文件

转载 作者:行者123 更新时间:2023-11-30 01:39:56 24 4
gpt4 key购买 nike

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()) ;
}
}

}

执行Demo后abc.txt的内容为:-

你好

无法运行程序“abc.txt”:CreateProcess error=32,该进程无法访问该文件,因为该文件正在被另一个进程使用

如何避免异常......

正如这里很多人建议的那样,我尝试了以下代码,但可悲的是,即使这样也会抛出异常......:-(

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()) ;
}
}
}

?????????

最佳答案

我假设调用 Runtime.getRuntime().exec(f.getPath()); 的原因是是在文本编辑器中打开 abc.txt 文件。最好提供打开文本编辑器的完整命令以及文件路径。我用 notepad.exe (windows) 尝试了这个,它成功了。

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()) ;
}
}
}
}

关于java - 执行动态创建的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1004601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com