找不到 .csv 文件

can't find .csv file

所以我的问题是我需要从我的程序中加载一个 .csv 文件 这是我的文件夹结构。

-IP1-Java ( workspace folder)
  -TP10 (program main folder)
      - Labyrinthe.java (main program)
      - Labyrinthe1.csv  (the file i have to "load")

但是当我 运行 Labyrinthe.java 时,我发现了这个:

java.io.FileNotFoundException: labyrinthe1.csv (The specified file can not be found)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at Labyrinthe.chargeLabyrinthe(Labyrinthe.java:11)
    at Labyrinthe.main(Labyrinthe.java:143)
D:\Bureau\Cours\Licence_Informatique_Diderot\Semestre-1\IP1-Java\labyrinthe1.csv

我翻译了一下,因为它是法语的

当我把labyrinthe1.csv文件放到IP1-Java文件夹中 它工作正常, 但这会在我的文件中造成混乱 所以我必须把与这个程序相关的所有东西都放在 TP10 文件夹中 我试过了:

-IP1-Java ( workspace folder)
  -TP10 (program main folder)
      - Labyrinthe1.csv  (the file i have to "load")
      -aaa 
          - Labyrinthe.java (main program)

我在TP10中创建了一个名为"aaa"的文件,并将Labyrinthe.java放在这里 文件是,但也没有用。

我必须说明我的程序没有问题,它是我的老师编写的,当我在学校的计算机上尝试时一切正常。 我什至删除了文件夹名称中的所有空格以防出现问题

我放置了加载文件的函数 正如我已经说过的,它是由我的老师编写的,并且有效

public static int[][]chargeLabyrinthe(final String nomFichier) {
    int[][] labyrinthe = {};
    try {
        Scanner sc = new Scanner(new File(nomFichier)).useDelimiter("\n");
        int c = 0;
        // On compte le nombre de lignes
        while (sc.hasNext()) {
            c = c + 1;
            final String tmp = sc.next();
        }
        labyrinthe = new int[c][];
        sc = new Scanner(new File(nomFichier)).useDelimiter("\n");
        int i = 0;
        while (sc.hasNext()) {
            final String ligne = sc.next();
            final String[] splitLigne = ligne.split(",");
            labyrinthe[i] = new int[splitLigne.length];
            for (int j = 0; j < splitLigne.length; j = j + 1) {
                labyrinthe[i][j] = Integer.parseInt(splitLigne[j]);
            }
            i = i + 1;
        }

    } catch (final Exception e) {
        System.out.println("Probleme dans la lecture du fichier " + nomFichier);
        e.printStackTrace();
    }
    return labyrinthe;
}

看起来您正在寻找的文件写成 Labyrinthe1.csv,但在您的异常中,它正在寻找的文件称为 labyrinthe1.csv

您确定使用的文件名正确吗?

我认为如果该文件与您的主 class(程序主文件夹)位于同一位置,并且这是一个结构为 src/main/java 的 java 项目,那么变量nomFichier 应该类似于 src/main/java/Labyrinthe1.csv

在Java中,可以使用标准的Maven项目结构(Maven是一个项目管理工具):

  • src: root source folder
    • main: main source folder
      • java: java sources classified by package
      • resources: resource files used in your different classes
    • test: test source folder
      • java: java sources used for testing src/main/java classes, using the same package classification
      • resources: test resource files
  • target: java compiled classes

在你的情况下,你会有类似的东西:

  • src
    • main
      • java
        • ip1.tp10.Labyrinthe: java source containing your method chargeLabyrinthe
    • test
      • java
        • ip1.tp10.LabyrintheTest: java source allowing to test your Labyrinthe object.
      • resources
        • tp10/Labyrinthe1.csv: the CSV file used for test scope.

并且使用该结构,您应该能够从 LabyrintheTest [=31] 加载资源文件 Labyrinthe1.csv =] 使用参数“tp10/Labyrinthe1.csv”.

希望对您有所帮助。