Note that there are some explanatory texts on larger screens.

plurals
  1. POexecuting a dynamically created file
    text
    copied!<pre><code>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()) ; } } } </code></pre> <p><strong>and the content of abc.txt after executing Demo is:-</strong></p> <p>Hello</p> <p>Cannot run program "abc.txt": CreateProcess error=32, The process cannot access the file because it is being used by another process</p> <p><strong>how to avoid the exception.....</strong></p> <p>as many people here suggested, i have tried the following code, but sadly, even that is also throwing excption....:-(</p> <pre><code>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()) ; } } } </code></pre> <p>??????????</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload