当我运行我的代码时,我收到NoSuchElementException错误。我希望输入是一个文件路径,将传递给File类,然后传递给BufferReader。
输出:
Enter directory:
java.util.NoSuchElementException
in java.util.Scanner.throwFor(Scanner.java:907)
in java.util.Scanner.next(Scanner.java:1416)
in test.atest(test.java:33)
in (OUWorkspace:1)
我的代码:
public void atest(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter directory: ");
String path = sc.next();
String pathname = OUFileChooser.getFilename(path);
File aFile = new File(pathname);
BufferedReader br = null;
try
{
String currentLine;
br = new BufferedReader(new FileReader(aFile));
currentLine = br.readLine();
while (currentLine != null)
{
System.out.println(currentLine);
currentLine = br.readLine();
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
br.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}
第33行是:String path = sc.next();