checking to see if a directory exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndedhia1
    New Member
    • Jan 2009
    • 112

    checking to see if a directory exists

    Hi.
    I am writing a java program in which I want to ftp a file to another unix box.
    First I have to check if the directory exists in which I am ftping into and if it does not exist, I have to create it:

    this is the code that I am using that is not working properly:

    [code=java]
    System.out.prin tln("YOU ARE IN UPLOAD");
    System.out.prin tln("THIS IS THE HOST: " + host);
    SshClient ssh = new SshClient();
    ssh.connect(hos t, 22);
    //Authenticate
    PasswordAuthent icationClient passwordAuthent icationClient = new PasswordAuthent icationClient() ;
    passwordAuthent icationClient.s etUsername(user );
    passwordAuthent icationClient.s etPassword(pass word);
    System.out.prin tln("upload user and password: " + user + password);
    int result = ssh.authenticat e(passwordAuthe nticationClient );
    if(result != AuthenticationP rotocolState.CO MPLETE){
    System.out.prin tln("Login to " + host + ":" + " " + user + "/" + password + " failed");
    }
    //Open the SFTP channel
    SftpClient client = ssh.openSftpCli ent();
    //Send the file
    //Debugging Print Statements
    System.out.prin tln("UPLOAD LOCALEFILE IS: " + localeFile);
    remoteDirFile = "/sbt/prod/infra/run_dir/tmp/nancy";
    System.out.prin tln("UPLOAD REMOTEDIRFILE IS: " + remoteDirFile);

    File devstorDir=new File(remoteDirF ile);
    boolean exists = devstorDir.exis ts();
    if (!exists) {
    // It returns false if File or directory does not exist
    System.out.prin tln("the file or directory you are searching does not exist : " + exists);
    System.out.prin tln("DEVSTORDIR is: " + devstorDir);
    boolean successCreating Dir = devstorDir.mkdi r();
    if (!successCreati ngDir) {
    // Directory creation failed
    System.out.prin tln("DIRECTORY: " + remoteDirFile + " WAS NOT CREATED: " + successCreating Dir);
    }
    else{
    System.out.prin tln("GREAT SUCCESS IN CREATING A DIRECTORY: " + successCreating Dir);
    System.out.prin tln("YOU ARE AT THE PUT NOW");
    client.put(loca leFile, remoteDirFile);
    }

    }else{
    // It returns true if File or directory exists
    System.out.prin tln("the file or directory you are searching does exist : " + exists);
    System.out.prin tln("DEVSTORDIR is: " + devstorDir);
    System.out.prin tln("YOU ARE AT THE PUT NOW");
    client.put(loca leFile, remoteDirFile);
    }



    //disconnect
    client.quit();
    ssh.disconnect( );
    [/code]

    I test the code by creating and deleting the directory on my test machine and then running the program and on each occasion, if the directory does or does not exist, I still get a false, saying that directory does not exist and it does not create a new directory.
    Since i am doing this inside of an FTP, does the mkdir() and the exists() methods not work or something?

    Thanks for the help!!!
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Have you checked the documentation for the SftpClient class? I would have guessed that the client instance is what lets you check for the existence of directories, create them and copy files etc. And not the Java File methods which are for naming files that are already part of your computer's file system.

    Comment

    • pbrockway2
      Recognized Expert New Member
      • Nov 2007
      • 151

      #3
      Just a guess but are we talking about com.sshtools.j2 ssh?

      If so, instead of "if(exists) {" you would say

      Code:
      client.stat(remoteDirFile);
      (I'm not sure if this is null if the directory doesn't exist, or whether it throws an IOException).

      There is also

      Code:
      client.mkdirs(remoteDirFile);
      which like it's Java counterpart does not fail if the directory already exists.

      Comment

      • ndedhia1
        New Member
        • Jan 2009
        • 112

        #4
        Hey pbrockway2,
        thanks a lot for your help!!
        I am working with com.sshtools.j2 ssh
        I got the mkdirs to work which makes the directory for me if it does not exist and if it does exist, it does nothing, which is what it is supposed to.
        I wasnt able to get the client.stat(rem oteDirFile) to work yet where i can check if a directory exists already, but I got most of what i needed to work!!
        I am still pretty new to java so i am trying to figure out how to create my own exception and what to do when an exception is throw. I catch it but I want to create the dir and then go back to where i need to be in the code. Still trying to figure that out.

        thanks again for the help!!!

        Comment

        • pbrockway2
          Recognized Expert New Member
          • Nov 2007
          • 151

          #5
          I've never used com.sshtools.j2 ssh (I just thew the class names at Google and had a look at what came back). The documentation says to use stat() before mkDirs() - I'm guessing but the reason might be so that you don't clobber a file with the same name as a directory on your new path. However they don't say how...

          Exceptions are well dealt with in Sun's Tutorial. Well worth knowing about, but my suggestion that stat() would throw an exception was just thinking aloud. Thinking about it today it seems to me a little counterintuitiv e for it to throw an exception: after all you are checking for the existence of a file and nonexistence is hardly exceptional. You're best bet would be to find some usage instructions for the package from whereever you downloaded it. Or get creative with Google.

          Anyway I'm glad you've got mkdirs() working as you want.

          Comment

          Working...