469,286 Members | 2,409 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

checking to see if a directory exists

112 100+
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:

Expand|Select|Wrap|Line Numbers
  1.            System.out.println("YOU ARE IN UPLOAD");
  2.            System.out.println("THIS IS THE HOST: " + host);
  3.            SshClient ssh = new SshClient();
  4.            ssh.connect(host, 22);
  5.          //Authenticate
  6.            PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
  7.            passwordAuthenticationClient.setUsername(user);
  8.            passwordAuthenticationClient.setPassword(password);
  9.            System.out.println("upload user and password: " + user + password);
  10.            int result = ssh.authenticate(passwordAuthenticationClient);
  11.            if(result != AuthenticationProtocolState.COMPLETE){
  12.                System.out.println("Login to " + host + ":" + " " + user + "/" + password + " failed");
  13.        }
  14.          //Open the SFTP channel
  15.            SftpClient client = ssh.openSftpClient();
  16.          //Send the file
  17. //Debugging Print Statements
  18.            System.out.println("UPLOAD LOCALEFILE IS: " + localeFile);
  19.            remoteDirFile = "/sbt/prod/infra/run_dir/tmp/nancy";
  20.            System.out.println("UPLOAD REMOTEDIRFILE IS: " + remoteDirFile);
  21.  
  22.            File devstorDir=new File(remoteDirFile);
  23.            boolean exists = devstorDir.exists();
  24.            if (!exists) {
  25.                   // It returns false if File or directory does not exist
  26.                   System.out.println("the file or directory you are searching does not exist : " + exists);
  27.                   System.out.println("DEVSTORDIR is: " + devstorDir);
  28.                   boolean successCreatingDir = devstorDir.mkdir(); 
  29.                   if (!successCreatingDir) { 
  30.                       // Directory creation failed 
  31.                       System.out.println("DIRECTORY: " + remoteDirFile + " WAS NOT CREATED: " + successCreatingDir);
  32.                       }
  33.                   else{
  34.                       System.out.println("GREAT SUCCESS IN CREATING A DIRECTORY: " + successCreatingDir);
  35.                                   System.out.println("YOU ARE AT THE PUT NOW");        
  36.            client.put(localeFile, remoteDirFile);
  37.                   }  
  38.  
  39.            }else{
  40.                   // It returns true if File or directory exists
  41.                   System.out.println("the file or directory you are searching does exist : " + exists);
  42.                   System.out.println("DEVSTORDIR is: " + devstorDir);
  43.                                 System.out.println("YOU ARE AT THE PUT NOW");        
  44.            client.put(localeFile, remoteDirFile);
  45.            }
  46.  
  47.  
  48.            //disconnect
  49.            client.quit();
  50.            ssh.disconnect();
  51.  
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!!!
Jan 13 '10 #1
4 13794
pbrockway2
151 Expert 100+
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.
Jan 13 '10 #2
pbrockway2
151 Expert 100+
Just a guess but are we talking about com.sshtools.j2ssh?

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

Expand|Select|Wrap|Line Numbers
  1. client.stat(remoteDirFile);
  2.  
(I'm not sure if this is null if the directory doesn't exist, or whether it throws an IOException).

There is also

Expand|Select|Wrap|Line Numbers
  1. client.mkdirs(remoteDirFile);
  2.  
which like it's Java counterpart does not fail if the directory already exists.
Jan 13 '10 #3
ndedhia1
112 100+
Hey pbrockway2,
thanks a lot for your help!!
I am working with com.sshtools.j2ssh
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(remoteDirFile) 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!!!
Jan 14 '10 #4
pbrockway2
151 Expert 100+
I've never used com.sshtools.j2ssh (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 counterintuitive 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.
Jan 14 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Rob Meade | last post: by
15 posts views Thread by Geiregat Jonas | last post: by
1 post views Thread by Kerem Gümrükcü | last post: by
9 posts views Thread by Sheldon Cohen | last post: by
1 post views Thread by ledder77 | last post: by
2 posts views Thread by John Smith | last post: by
11 posts views Thread by Daz | last post: by
reply views Thread by emp | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.