473,378 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 14827
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

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

Similar topics

3
by: Rob Meade | last post by:
Hi all, I am allowing a user to create a directory within my application. I have them entering the name for the directory in a form. When the form is processed I initially check for any...
15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
1
by: Kerem Gümrükcü | last post by:
Hi, how can i find out, whether a given string specifies a Path or a file... System.IO.File.Exist(UnknownString) does not work for my application. i exactly need to know if the strng is a path...
9
by: Sheldon Cohen | last post by:
Hello, I am using c# and running a site that is on a shared host. The code in question is supposed to create a new directory that is coming out of a text box. It works fine on my computer, but...
1
by: ledder77 | last post by:
Hi, I need to find a way to check if a relative url exists on the server. I cannot use WebRequest.Create method because it needs a complete URI. Any suggestion? Thanks in advance Francesco
2
by: John Smith | last post by:
1. Which is the best way to check the existence of a given directory using vb.net? .Net 1.1. 2. Which is the best way to check the existence of a given file using vb.net? .Net 1.1. Drive...
11
by: Daz | last post by:
Would anyone know of a way to check whether or not a particular path is a directory? If I have to go down the route of using _findfirst and _findnext (windows specific), then so be it. However, I...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
0
by: emp | last post by:
I am writing a logging function. Hourly a log entry is made into a directory for the current date. Each day a new current date directory is made. How can I check to see if a directory exists?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.