【问题标题】:File not found exception - Java [duplicate]找不到文件异常 - Java [重复]
【发布时间】:2014-12-02 00:02:19
【问题描述】:

我正在编写一个带有并行数组的代码,当我读入数据并尝试显示它时,会出现此错误。教我实验室的 GA 没有看到我的代码有问题,并说我应该在这里尝试。这是我的代码,错误将紧随其后。我正在使用日食。

import java.util.Scanner; 
import java.io.File;
import java.io.FileNotFoundException;
public class parallelArrays {
    public static void main(String[] args) throws FileNotFoundException {
        File cityPopulation = new File("cityPopulationData.txt");
        Scanner fileReader = new Scanner(cityPopulation);
        fileReader.useDelimiter("[\t|\n]+");
        String[] cities = new String[400];
        int[] pop2010 = new int[400];
        int[] pop2013 = new int[400];
        double[] area = new double[400];
        int count = getData(fileReader, cities, pop2010, pop2013, area);
        displayArrays(cities, pop2010, pop2013, area, count);
        largestCity(pop2010, count);
    }

    public static int getData(Scanner inf, String[] c, int[] pop10, int[] pop13, double[] a) {
        int count = 0;
        inf.next();
        inf.next();
        inf.next();
        inf.next();
        while(inf.hasNext()) {
            c[count] = inf.next();
            pop10[count] = inf.nextInt();
            pop13[count] = inf.nextInt();
            a[count] = inf.nextDouble();
            count++;
        }
        return count;
    }

    public static void displayArrays(String[] c, int[] pop10, int[] pop13, double[] a, int count) {
        for(int i = 0; i < count; i++){
            System.out.printf("%s \t %d \t %d \t %f", c[i], pop10[i], pop13[i], a[i]);
        }
    }

    public static int largestCity(int[] pop10, int count) {
        int lCindex = 0;
        for(int i = 1; i < count; i++) {
            if(pop10[i] > pop10[lCindex])
                lCindex = i;
        }
        return lCindex;
    }

    // public static int findGrowth(int[] pop10, int[] pop13, int count,  ) {
    // 
    // }

    public static int highestDensity(int[] pop10, double[] area, int count) {
        int hDindex = 0;
        for( int i = 1; i < count; i++) {
            if ((pop10[i]/area[i]) > (pop10[hDindex]/area[hDindex]))
                hDindex = i;
        }
        return hDindex;
    }
}

例外:

Exception in thread "main" java.io.FileNotFoundException: cityPopulationData.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at parallelArrays.main(parallelArrays.java:7)

【问题讨论】:

  • 尝试使用绝对路径
  • 或将文件保存在项目文件夹中
  • 老师把你送到SO。经典。
  • @Takendarkk 不是老师,GA,我敢肯定老师会说出 cmets 所说的话。
  • 我的文件在项目文件夹中

标签: java eclipse file-io


【解决方案1】:

这个异常是不言自明的“java.io.FileNotFoundException”
==>文件 cityPopulation = new File("cityPopulationData.txt");

由于您使用的是 Eclipse,请尝试提供文件的绝对路径,例如 c:/data/filex.txt 等。它会起作用。之后在您的项目树中创建一个资源文件夹并将文件放在那里。使用 ClassLoader.getresouceAsStream 加载文件。当您想要相对访问您的文件时使用以下方法..您可以在线获取有关此方法的更多信息。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 2013-08-02
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 2023-03-24
    相关资源
    最近更新 更多