gpt4 book ai didi

java - 找不到 .csv 文件

转载 作者:行者123 更新时间:2023-12-01 19:52:39 25 4
gpt4 key购买 nike

所以我的问题是我需要从我的程序加载 .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;
}

最佳答案

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

使用该结构,您应该能够使用参数“tp10/Labyrinthe1.csv”从 LabyrintheTest 类加载资源文件 Labyrinthe1.csv ”。

希望这对您有帮助。

关于java - 找不到 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59072844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com