• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

A little java help

Status
Not open for further replies.

Darias

Member
Code:
InputStreamReader reader = 
					new InputStreamReader(System.in); 
				BufferedReader in = new BufferedReader(reader);
				path = in.readLine();
				System.out.println();

Running this through a debugger, I notice that when it is given c:\ it fills path with c:\\

I can't find any documentation as to why its giving me an extraneous backslash, nor does it happen when given another directory, for example c:\dev


Any ideas?
 

Slo

Member
Maybe because '\' is the escape character. So C:\dev basically is read as "C:"+ \d +"ev". The double slash is interpreted as "don't interperate the second '\' as an escape character." When you specify a path to a file you should either do C:/dev or C:\\dev.
 

Darias

Member
Ahhh... would that mean that String is storing a char[], and to fix it properly I would need to write a little input formatter for paths ??

Also, would you mind answering a longer, more involved question regarding extensions of the File class?
 

Slo

Member
nah, you don't need to write anything to fix the String class. Just use either of my examples.

C:\\directory\\subdirectory\\file.extension
C:/directory/subdirectory/file.extension

The second is more unix friendly, since you'll probably be using paths relative to some root.

static final String MY_FILE_PATH="/directory/subdirectory/file.extension";
File myfile= new File(SYSTEM_ROOT_VARIABLE + MY_FILE_PATH);

As for your question, I'm not a java expert, but you might as well post it. If I can't answer it, someone else probably can.
 
java.lang.File has a public static String field called pathSeparator. You should use that
in leiu of any hardcoded path separators in your code-it will resolve properly depending on the platform you are working on.

There's also a character field called pathSeparatorChar, if you'd prefer to use that.
 
Status
Not open for further replies.
Top Bottom