Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: check to see if a file exists using the current directory

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default check to see if a file exists using the current directory

    I have a program, that after the user puts in some information, they click a button to exectute the program. I want the code to check to see if a file exists before executing the program.
    I need my program to look in the same directotry that it is running from for a .lic file. Then if it does not exeist to alert the user that there is no license installed and to follow the readme instrucitons found in the running directory.

    Here is my coding for the button execution.
        private void btnRunRelapActionPerformed(java.awt.event.ActionEvent evt) {                                            
          String in = tfIntdta.getText();
          String rst = tfRstplt.getText();
          String out = tfOutdta.getText(); 
          String strip = tfStpdta.getText();
          String guistring = "-n gui";
          String wd = System.getProperty("user.dir");
          String osver = System.getProperty("os.name");
          String exec = "";
            if (osver.contains("Mac OS X")){
               exec = "/Users/brian.allison/rs40/relap5.x";
            } else if (osver.contains("linux")) {
               exec = "relap5.x";
            } else if (osver.contains("windows")) {
                exec = "relap5.exe";
     
            }
     
           try{
     
            String Fileout = tfOutdta.getText();
            //Delete if tempFile exists
            File fileout = new File(Fileout);
              if (fileout.exists()){
                 fileout.delete();
              }   
          }catch(Exception e){
              // if any error occurs
     
          }
            //
            if (cbDelRstplt.isSelected()) {
             try{
     
            String Filerst = tfOutdta.getText();
            //Delete if File exists
            File filerst = new File(Filerst);
              if (filerst.exists()){
                 filerst.delete();
              }   
          }catch(Exception e){
                 // if any error occurs
     
          }   
            }
            String[] cmd = {exec, "-i", in, "-r", rst, "-o", out};
            if (tfStpdta.isVisible()) {
            String[] cmd2  = {exec, "-i", in, "-r", rst, "-o", out, "-s", strip};
            cmd = cmd2;
            } else if (cb3DGui.isSelected()) {
             String[] cmd3  = {exec, "-i", in, "-r", rst, "-o", out, "-n", "gui"};
             cmd = cmd3;
            } 
            System.out.println(strip);
        if 
     
    //
            try{
            Runtime.getRuntime().exec(cmd);
     
            } catch (IOException ex) {
          }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: check to see if a file exists using the current directory

    Check out the File class in the Java JDK.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: check to see if a file exists using the current directory

    Quote Originally Posted by jim829 View Post
    Check out the File class in the Java JDK.

    Regards,
    Jim
    I am a newb to Java programming. where do I find info on file class.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: check to see if a file exists using the current directory

    Here is link to the API doc: https://docs.oracle.com/javase/8/docs/api/index.html
    Find the entry for the File class in the lower left window, click on it and the API doc for the class will show in the main window.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java.lang.Exception: folder exists, but it is not a directory.
    By tandonkunal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2014, 11:49 PM
  2. How to check if a session object exists (or is not null)?
    By BimmyJim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 6th, 2014, 10:10 AM
  3. [SOLVED] Check if system tray already exists
    By keepStriving in forum Java Theory & Questions
    Replies: 25
    Last Post: December 17th, 2013, 06:37 PM
  4. How do you check to see if a RandomAccessFile already exists?
    By ineedahero in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 09:12 AM
  5. check if table exists from a list of files in a directory
    By efoikonom in forum JDBC & Databases
    Replies: 1
    Last Post: June 10th, 2013, 08:42 AM