Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem inputting a directory path

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I have a class that searches a directory specified from the command line. My problem is, when I try to enter a path like &quot;c:\java&quot; it gives me an error. I figured out I have to use an escape character like &quot;c:\\java&quot; and then it works. I tried to build something internal to the program so that a user could just type in the one \ and not have to worry about doubling it up. Unfortuneately, the \ behaves as a escape charactor. When I try using my replace method like this: replace(dir, &quot;\&quot;, &quot;\\&quot;) it doesn't like this format. Is there another way that I can specify the directory? Using .'s perhaps? <br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
Dear fenris,<br><br>You are misunderstanding the concept of escape characters. The leading backslash indicates an escape sequence where the next character determines the character to be placed into memory. So if you want a line feed as a character in memory your literal string is: &quot;hello\n&quot; Now in memory there is [68 65 6C 6C 6F 0A] The compiler places the 0A byte to replace your escape sequence.<br><br>Since the backslash character is used as the escape sequence delimiter you must use two in a string literal to get the ONE in memory. Once in memory it is used correctly by all the 'directory' related operations.<br><br>Bottom line is... you don't want to add the second one to the memory variable generated by the user interface. <br><br>But let's say you did want to add one where there was none! You could do this:<br><br>String part1 = &quot;C:&quot;;<br>String part2 = &quot;myfile.txt&quot;<br>String path = part1 + 0x5C + part2;<br><br>now path = &quot;C:\myfile.txt&quot;;<br><br>Hope this helps<br>-pete
 
I am not sure that I quite understand you response Pete. I know what an escape character is supposed to do. But, what I have is a class that searches through a directory for particular file name patterns. It is a command line interface that requires one argument, the directory to search. The problem is that when (in winNT) the programm is envoked: java tree &quot;c:\java&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;It complains that it is missing an argument (I have some error checking) I found that I have to manually add the second \ (ie &quot;c:\\java&quot;) inorder to even get the input into the program! Besides I am wondering if there is a more universal way to specify paths (UNC?) that would be easy for a user to input and follow (I am assuming more windows user that are familiar with c:, d:, etc. would be using it). <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
Dear fenris,<br><br>We must be talking about different things. Here is what I am referring to. Let's say the user runs this app and passes the string C:\Files on the command line, which referes to a directory on the 'c' drive, i.e.:<br><br>java myapp c:\files<br><br>Then the following code would place the incoming argument 'as is' in the constructor of the class java.io.File<br><br>The constructor has no problem reading the string with a single backslash character.<br>import java.io.*;<br>... other java stuff<br><br>public static void main (String[] args)<br>{<br>&nbsp;&nbsp;System.out.println( args.length);<br>&nbsp;&nbsp;if ( args.length &gt; 0){<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;File f = new File(args[0]);<br>&nbsp;&nbsp;&nbsp;&nbsp;if ( f.isDirectory()){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String[] flist = f.list();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i&lt;flist.length; i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println( flist<i>);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println( f.lastModified());<br>&nbsp;&nbsp;}<br>}<br><br>Hope this helps<br>-pete
 
It works now??? I don't know what happened or how dumb I am, but it works with out doing the double backslash deal. When I was trying it yesterday, it would always give missing argument errors (I was just checking the length of args, making sure that it held the proper variables). The only way I could get it to work was to put the double slashes in. I even put in a system.out.println to see what the inputed results looked like to the programm before anything was done to it. <br><br>For example I would enter &quot;c:\\java&quot; and the program would spit out &quot;c:\java&quot; so I am not quite sure what was happening. Could it be the fact that my programm required to commandline arguments?<br><br>Boy did I ever making a dumb mistake....<br><br>Thanks for the help Pete, I appreciate it... <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top