0

I have a Java program that prompts user for input file path and name, the program will read from that file:

Scanner inScanner = new Scanner(System.in);
inScanner.useDelimiter("\n");
System.out.print("Enter input file path and name:");
String inFile = inScanner.next();
System.out.println("You entered: " + inFile);           
FileInputStream fs = new FileInputStream(inFile); // FAILED HERE

When I ran this program within Eclipse, I got the "syntax is incorrect" error:

Enter input file path and name: C:\\Users\\My Work\\myFile.txt
You entered: C:\\Users\\My Work\\myFile.txt
(The filename, directory name, or volume label syntax is incorrect)rks\myFile.txt

But it worked fine if I gave the same path hardcoded in the same java program and created FileInputStream from this hardcode value:

 private static final String inFile = "C:\\Users\\My Work\\myFile.txt";

So how do I give this path from input in Eclipse? Is it because I have space in the path? I have tried using double quotes around the input file path, or single \, or single / in the file path, it gave me the same error. Thanks.

ppovoski
  • 3,493
  • 5
  • 20
  • 27
jlp
  • 1,120
  • 2
  • 24
  • 39
  • you may have to treat the path as a string surrounded by double quotes. perhaps try "\"C:\\Users\\My Work\\myFile.txt\"" because that how I see the command prompt treat any path that has a space in the directory name – RAZ_Muh_Taz Jan 07 '17 at 00:22
  • http://askubuntu.com/questions/530578/how-to-write-the-path-of-a-folder-with-space-in-its-name – RAZ_Muh_Taz Jan 07 '17 at 00:26
  • Just tried it. Still didn't work. – jlp Jan 07 '17 at 00:26
  • you are 100% sure that you have a user called "My Work"? because that seems to be a very odd name for a user. Can you navigate to the file in a cmd/terminal using that exact path? – RAZ_Muh_Taz Jan 07 '17 at 00:27
  • @ RAZ_Muh_Taz FileInputStream just take a string as argument and there is no such a string inside another double quotes – Khalil M Jan 07 '17 at 00:35
  • *Why* did you enter a file path with double backslashes?? A Windows file path only uses single backslashes. Double backslashes are only needed in Java code when write a `String` *literal*. Command-line inputs are not string literals. – Andreas Jan 07 '17 at 00:35

1 Answers1

2

The line inScanner.useDelimiter("\n"); causes the problem. If you delete it, the code works, regardless of using \ or \\ in the file path.

The method useDelimiter(String pattern) uses the given String as a regular expression pattern, which behaves differently than your everyday String escape sequence:

An invocation of this method of the form useDelimiter(pattern) behaves in exactly the same way as the invocation useDelimiter(Pattern.compile(pattern)).

In Java regular expressions, both \r (carriage return) and \n (linefeed) are used as line separators. On Unix, \n would work, but on Windows, you need \r\n. So inScanner.useDelimiter("\r\n"); would work, too. You can read more on this issue in related posts here and here.

However, the safe variant is to just use inScanner.nextLine(); instead of the delimiter pattern and next();, so you should definitely stick to that, since it is exactly made for this purpose and regular expressions are hard to master.

Community
  • 1
  • 1
thatguy
  • 13,242
  • 6
  • 19
  • 33
  • Thanks. It worked. Can you please let me know why I can't use \n as delimiter ? – jlp Jan 07 '17 at 00:37
  • Added an explanation. Just use `nextLine()`, which uses newline as a delimiter. It is far easier. – thatguy Jan 07 '17 at 00:57