我只是试图读取一个文件,我的类文件与我试图读取的文件存在于完全相同的目录中。我尝试读取的文件名为profiles.txt。我以前在极其相似的情况下做过完全相同的方法,并且它起作用了(现在仍然起作用),我不知道为什么这不起作用。如果有人能解释一下,我将非常感激。
public static void readProfiles(BST tree) {
try {
BufferedReader getData = new BufferedReader(
new FileReader(
new File("profiles.txt")));
String data = getData.readLine();
while(data != null) {
String[] profileData = data.split(",");
String[] interests = profileData[7].split(";");
tree.insertProfile(new Profile(
profileData[0],
new int[] {Integer.parseInt(profileData[1]), Integer.parseInt(profileData[2]), Integer.parseInt(profileData[3])},
profileData[4],
profileData[5],
profileData[6],
interests
));
data = getData.readLine();
}
getData.close();
}
catch(FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}
catch(IOException e) {
System.out.println("IO error occured");
System.exit(0);
}
}发布于 2018-05-07 08:02:19
文件名是相对的,并且不包含任何目录,因此它需要位于当前工作目录中。
类文件所在的位置与此无关。
发布于 2018-05-07 08:03:12
尝试相对于程序的主运行文件的路径。
https://stackoverflow.com/questions/50205551
复制相似问题