0
我正在制作一个使用文本文件的程序。我需要这样做,以便程序可以使用其jar文件从另一台计算机上运行。问题是,我无法找到正确的文件路径到文本文件。我试过使用getResource()
,但它仍然不能正常工作。代码如下:无法在Netbeans项目中获取文件路径
public class Params {
public static void init() {
hsChartSuited = new int[13][13];
file = new File(Params.class.getResource("HandStrengthDataSuited.txt").getFile());
try {
Scanner input = new Scanner(file);
for (int i = 0; i < hsChartSuited.length; i++) {
for (int j = 0; j < hsChartSuited[i].length; j++) {
hsChartSuited[i][j] = Integer.parseInt(input.next()) - 20;
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
}
HandStrengthDataSuited.txt
是在我的项目的src文件夹中的文件。它也位于文件夹之外,也位于项目的主目录中。我试着打印文件的绝对路径,这就是我得到:
/Users/MyUsername/file:/Users/MyUsername/Documents/Homework_Soph_2012/Computer%20Science/HoldEm/dist/HoldEm.jar!/holdem/HandStrengthDataSuited.txt
,我需要得到的文件路径是
/Users/MyUsername/Documents/Homework_Soph_2012/Computer%20Science/HoldEm/holdem/HandStrengthDataSuited.txt
有谁知道问题是什么吗?
这样,我取代''组织/ paulvargas /资源/ file.txt'的src /扑克/ HandStrengthDataSuited .txt“呢? – RaysonK
只有'holdem/HandStrengthDataSuited.txt'如果src不是java数据包。 –