Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 247053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:13:26+00:00 2026-05-11T21:13:26+00:00

import java.io.*; public class Demo{ public static void main(String[] args){ File f = new

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

}

and the content of abc.txt after executing Demo is:-

Hello

Cannot run program “abc.txt”: CreateProcess error=32, The process cannot access the file because it is being used by another process

how to avoid the exception…..

as many people here suggested, i have tried the following code,
but sadly, even that is also throwing 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()) ;
        } 
    }
}

??????????

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:13:26+00:00Added an answer on May 11, 2026 at 9:13 pm

    I am assuming that the reason for calling Runtime.getRuntime().exec(f.getPath()); is to open up the abc.txt file in a text editor. It would be better to provide complete command for opening the text editor along with the file path. I tried this with notepad.exe (windows) and it worked.

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

    Following code dynamically generates java code and uses javac to compile it

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do what you want by removing the current… May 12, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer Ditto to Phillipe's answer. you didn't give us the whole… May 12, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer It's probably starting to look like I'm promoting this.... Try… May 12, 2026 at 4:18 pm

Related Questions

So my 2009 new years resolution is to learn Java. I recently acquired Java
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Here is a simple piece of code: import java.io.*; public class Read { public
Here is the code I have thus far: import java.io.*; class JAVAFilter implements FilenameFilter

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.