简体   繁体   English

为什么在此程序中出现java.io.FileNotFoundException?

[英]Why am I getting java.io.FileNotFoundException in this program?

Why am I getting java.io.FileNotFoundException in the following program? 为什么在以下程序中出现java.io.FileNotFoundException?

import java.io.*;

class FisDemo {

    public static void main(String[] args)throws IOException{

        FileInputStream fis=new FileInputStream("abc.txt");
/* Here we are accessing file abc.txt statically. i.e abc.txt must exist in current class directory */

        int  data;

        while(( data=fis.read())!=-1){
            System.out.println((char)data);
      // here we are casting, because return type of read() is int
        }
    }
}

FileInputStream fis=new FileInputStream("abc.txt"); FileInputStream fis = new FileInputStream(“ abc.txt”);

Most likely you have bad file path . 很可能您的file path
So first you need to check where your file is located and then add right path. 因此,首先您需要检查文件的位置,然后添加正确的路径。

Have also look at: How to construct a file path in Java or Construct file path . 还看过: 如何在Java中 构造文件路径构造文件路径

The program cannot find the abc.txt file. 该程序找不到abc.txt文件。 Maybe it is not present in your output / bin folder. 也许它不在您的output / bin文件夹中。 It depends on your IDE if the file can be in src folder / resource folder or has to be copied to output folder after building the application. 该文件是否可以位于src文件夹/资源文件夹中,或者必须在构建应用程序后复制到输出文件夹,这取决于您的IDE。

The reason is abc.txt is not present(in the current directory). 原因是不存在abc.txt(在当前目录中)。

FileInputStream fis=new FileInputStream("abc.txt");

Specify the full name of the file 指定文件的全名

    String fileFullName="/home/abc.txt";
    FileInputStream fis=new FileInputStream(fileFullName);

And yes is the answer for your second question. 是的,这是第二个问题的答案。

该文件在当前的Java文件目录中不存在。请在该目录中创建然后尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM