我想使用scanner读取文件及其目录。问题是目录不能通过扫描仪读取,因为其中有一个空白区域。我尝试用S.nextLine()
处理它,但它不会跳过执行并跳到其余的代码。
这是代码:
public class readFile {
public static void main (String args[]){
System.out.print("Enter test file directory: ");
testFile = S.nextLine();
//testFile = "C:\\Users\\firstname lastname\\Desktop\\test1.txt"; This works
File test = new File(testFile);
try
{
Scanner readFile = new Scanner(test);
while(readFile.hasNextLine())
{
int i = readFile.nextInt();
System.out.println(i);
}
readFile.close();
}catch(FileNotFoundException e){System.out.println("File doesn't exist");}
}
}