繁体   English   中英

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

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

我知道有些人已经遇到过这个问题,但我仍然无法解决。

这是我的代码:

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...

基本上,我将我的文本文件复制到项目目录中,在工作区主目录中的子目录(指定路径)中,但Eclipse继续抛出FileNotFoundException 此外,我已尝试刷新左栏中的项目文件,但没有结果。

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();
            }
        }

    }

相对文件位置

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

尝试使用文件的绝对路径。 如果您使用IDE将文件放在src文件夹中或放在单独的文件夹中,并将文件路径指定为foldername / filename。

暂无
暂无

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

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