引用符付きのファイル名と引用符で囲まれていないファイル名を入力しようとしましたが、最初のスペースまでしか処理しませんでした。パスに空白がない場合でも、ファイルは検出されませんが、パスは正しく表示されます。それを修正するには?"ファイル名、ディレクトリ名、またはボリュームラベルの構文が正しくありません"
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をメインに追加するか、代わりに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();
スペースを含むファイル名を読み取ろうとしていますか? –
パスにスペースが含まれています。 – user93200
'next()'の代わりに 'nextLine()'を使うか、@MДΓΓLLLLLLの提案に従ってください。 – chrisn