Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到带有FileInputStream的文件_Java_File_Java.util.scanner_Filenotfoundexception_Fileinputstream - Fatal编程技术网

Java 找不到带有FileInputStream的文件

Java 找不到带有FileInputStream的文件,java,file,java.util.scanner,filenotfoundexception,fileinputstream,Java,File,Java.util.scanner,Filenotfoundexception,Fileinputstream,我正在尝试检查我的程序是否正在扫描文件的内容,但出现以下错误: Exception in thread "main" java.io.FileNotFoundException: input.txt (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io

我正在尝试检查我的程序是否正在扫描
文件的内容
,但出现以下错误:

Exception in thread "main" java.io.FileNotFoundException: input.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at lottery.main(lottery.java:40)
线程“main”java.io.FileNotFoundException:input.txt中的异常(系统找不到指定的文件) 位于java.io.FileInputStream.open0(本机方法) 位于java.io.FileInputStream.open(未知源代码) 位于java.io.FileInputStream。(未知源) 在lotify.main(lotify.java:40) 我没有看到代码中的问题,因为我总是这样处理文件,似乎无法理解这个问题

代码:

publicstaticvoidmain(字符串[]args)抛出FileNotFoundException
{
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入带有票据数据的文件名”);
字符串输入=in.nextLine();
文件=新文件(输入);
in.close();
扫描仪扫描=新扫描仪(新文件输入流(文件));
int lim=scan.nextInt();
对于(int i=0;i
检查是否从输入文件所在的目录启动jvm

如果没有,就不可能找到相对路径。最终将其更改为绝对路径(类似于/usr/me/input.txt)


如果文件位于启动java程序的目录中,请检查文件的权限。对于启动java程序的用户,它可能不可见。

问题是您的程序在当前工作目录中找不到
input.txt


查看程序运行的目录,检查其中是否有名为
input.txt的文件。

文件输入流从文件系统中的文件获取输入字节。哪些文件可用取决于主机环境。
From

这意味着您的FileInputStream需要提供一个实际的文件系统文件。但您仅在调用new File()时创建了一个filehandler。因此,需要在文件系统上创建文件,调用file.createNewFile()

public static void main(String[] args) throws FileNotFoundException 
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the name of the file with the ticket data.");
    String input = in.nextLine();
    File file = new File(input);

    in.close();

    Scanner scan = new Scanner(new FileInputStream(file));
    int lim = scan.nextInt();
    for(int i = 0; i < lim * 2; i++)
    {
        String name = scan.nextLine();
        String num = scan.nextLine();

        System.out.println("Name " + name);
    }

    scan.close();
}
File file = new File(input); //here you make a filehandler - not a filesystem file.

if(!file.exists()) {
    file.createNewFile(); // create your file on the file system
} 

Scanner scan = new Scanner(new FileInputStream(file)); // read from file system.