2

我很确定这有一个简单的原因,但是在梳理谷歌点击后我无法弄清楚。

问题:我试图从我创建并放置在 java 项目的 src 文件夹中的 .dat 文件中读取,但 eclipse 无法识别它。

我尝试过的事情,1.令人耳目一新的项目。2.在很多地方手动放置文件。3.保存并重新启动。

数据文件

2
12087 400
7418 978

代码

import java.io.*;
import java.util.*;

public class Distance {

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

Scanner q = new Scanner (new File("distance.dat"));

int count = Integer.parseInt(q.nextLine().trim());

System.out.println(count);

   }

}

包资源管理器 包资源管理器

调试错误 调试错误

4

1 回答 1

1

对我来说,它看起来像 distance.dat 在 src 文件夹中,这意味着你需要做

public static void main(String[] args) throws IOException {
    Scanner q = new Scanner (new File("src/distance.dat"));
    int count = Integer.parseInt(q.nextLine().trim());
    System.out.println(count);
}

这是因为 Eclipse 在项目文件夹中启动,而不是在 src 文件夹中启动。

我最喜欢的调试方法是:

public static void main(String[] args) throws IOException {
    File f = new File("src/distance.dat");
    System.out.println(f.getAbsolutePath());  //debug here that it's point to the right file
    Scanner q = new Scanner (f);
    int count = Integer.parseInt(q.nextLine().trim());
    System.out.println(count);
}
于 2015-03-09T20:47:25.830 回答