从文本文档读取时FileNotFoundException

从文本文档读取时FileNotFoundException,exception,java.util.scanner,Exception,Java.util.scanner,我正试图从中读取一个文件 public static void printLines(String doc){ Scanner input = null; input = new Scanner(doc + ".txt")); while(input.hasNext()){ String nextLine = input.nextLine(); System.out.println(nextLine); } in

我正试图从中读取一个文件

public static void printLines(String doc){

    Scanner input = null;
    input = new Scanner(doc + ".txt")); 

    while(input.hasNext()){

        String nextLine = input.nextLine();
        System.out.println(nextLine);

    }

    input.close;

}
上面的代码来自一个方法,该方法将字符串doc作为参数,并打印出每一行。在我的main中,我使用try-catch块捕获FileNotFoundException。但是,当我尝试运行该程序时,它始终会遇到FileNotFoundException


我使用的是Eclipse,文本文件和java文件都在同一个“java项目”中。我的方法无法读取文本文件有什么原因吗?请帮忙

获取FileNotFoundException的原因是使用了错误的Scanner构造函数

更改
input=新扫描仪(doc+“.txt”)


input=新扫描仪(新文件(doc+“.txt”)应该适合您的需要。

尝试在其中打印
文档。它可能还包括文件的扩展名

例如,如果文档是
/home/user/Desktop/mySample.txt
,扫描仪将尝试查找不可用的
/home/user/Desktop/mySample.txt
,因为您再次连接扩展名
.txt