简体   繁体   English

Eclipse,无法读取Java类中的文本文件

[英]Eclipse, Unable to Read Text File in Java Class

I know that some people have already encountered this problem, but I am still unable to solve it. 我知道有些人已经遇到过这个问题,但我仍然无法解决。

Here is my code: 这是我的代码:

import java.io.*;

public class MainClass
{
    public static void main( String[] args )
    {
       BufferedReader br = new BufferedReader(new FileReader("dump.txt"));
       String line;
       int i = 0;
       int j = 0;
       while((line = br.readLine()) != null){

// And so on...

Basically, I copied my text file in the project directory, in a subdirectory (specifying the path), in the workspace main directory, but Eclipse continues to throw a FileNotFoundException . 基本上,我将我的文本文件复制到项目目录中,在工作区主目录中的子目录(指定路径)中,但Eclipse继续抛出FileNotFoundException Besides, I have already tried with refreshing the project files in the left column, but without results. 此外,我已尝试刷新左栏中的项目文件,但没有结果。

public static void main(String[] args) {

        BufferedReader br = null;
        try {
            String sCurrentLine;
//          br = new BufferedReader(new FileReader("D:\\test.txt")); //Absolute File
            br = new BufferedReader(new FileReader("test.txt"));//Relative File
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

Relative file location 相对文件位置

 MyProject
 --src
    --Main
 --test.txt

Try with your absolute path of the file. 尝试使用文件的绝对路径。 If you are using IDE place the file in your src folder or place in separate folder and give the file path as foldername/filename. 如果您使用IDE将文件放在src文件夹中或放在单独的文件夹中,并将文件路径指定为foldername / filename。

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

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