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.

6
  • 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 Commented Jan 7, 2017 at 0:22
  • askubuntu.com/questions/530578/… Commented Jan 7, 2017 at 0:26
  • Just tried it. Still didn't work. Commented Jan 7, 2017 at 0: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? Commented Jan 7, 2017 at 0:27
  • @ RAZ_Muh_Taz FileInputStream just take a string as argument and there is no such a string inside another double quotes Commented Jan 7, 2017 at 0:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It worked. Can you please let me know why I can't use \n as delimiter ?
Added an explanation. Just use nextLine(), which uses newline as a delimiter. It is far easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.