Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Command Line Argument issue

843789Feb 23 2010 — edited Feb 24 2010
Hi,

I was creating a program which list the files in directory name, which is passed from the command line.

I have observed the File constructor accepts paths with forward slash "\" at the end of the directory path. I mean the following are valid arguments to File constructor. and works fine

1) C:\Program Files
2) C:\Program Files\


But, while running from command line (windows platform) I need to use quotes (") around the directory path, is the path contains spaces (Eg:- "C:\Program Files").

"C:\Program Files" (with quotes) is a valid argument to File constructor.
And "C:\Program Files\" (with quotes) IS NOT a valid argument to File constructor.

Here is the coding I used validate this scenario.

{code}
if((args[0].charAt(args[0].length()-1)=='"'))
if(args[0].charAt(args[0].length()-2)=='\\'){
System.out.println("Please remove: \\ from the path");
return;
}
....
....
File f = new File(args[0]);
args[0].charAt(args[0].length()-1) is correctly returns double quote (").

The issue I am facing is, so I am not able to check the shash.

args[0].charAt(args[0].length()-2)  DOES NOT return slash character  '\\'  (actually, returns the character before the slash)


Please help me if there is any work around solution                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2010
Added on Feb 23 2010
46 comments
338 views