2012-03-01 29 views
-1

따옴표 붙은 파일 이름이나 인용되지 않은 파일 이름을 입력하려고했지만 여전히 첫 번째 공간까지만 처리합니다. 경로에 공백이 없더라도 파일은 감지되지 않지만 경로는 올바르게 표시됩니다. 그것을 고치는 방법?"파일 이름, 디렉터리 이름 또는 볼륨 레이블 구문이 올바르지 않습니다."

Enter the filename: a b c 
java.io.FileNotFoundException: a (The system cannot find the file specified) 
Enter the filename: "a b c " 
java.io.FileNotFoundException: "a (The system cannot find the file specified) 

파일 입력을 얻는 가장 좋은 방법입니까? 또한 IOException, FileNotFoundException을 main에 추가하거나 try {}를 사용하여 추가해야합니까?

System.out.print("Enter the filename: "); 

Scanner stdin = new Scanner((System.in)); //Keyboard input 
String nextDataValue, data, popped="", tag="", fileName=stdin.next(); 

FileInputStream fis = null; 
    try { 
     fis = new FileInputStream(fileName); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
InputStreamReader inStream = new InputStreamReader(fis); 
BufferedReader in = new BufferedReader(inStream); 

data=in.readLine(); 
+0

공백이있는 파일 이름을 읽으려고합니까? –

+0

경로에 공백이 있습니다. – user93200

+2

'next()'대신'nextLine()'을 사용하거나 @ MДΓΓLLLLLL의 제안과 함께하십시오. – chrisn

답변

3

스캐너는 공백으로 구분 된 토큰을 제공합니다. the Scanner JavaDocs에서 :

Scanner 기본적으로 공백 일치하는 구분자 패턴을 사용해 입력을 토큰에 분할합니다.

그래서 여기 있습니다. 나는 말하기 싫지만 RTFD의 경우입니다.

다른 구분 기호를 사용하거나 Scanner#next() 대신 Scanner#nextLine()을 사용하십시오.

+0

그러면 더 많은 정보를 제공해야합니다. –

관련 문제