【发布时间】:2013-12-17 21:00:56
【问题描述】:
嗨,我做了一个 Java 程序,它从 CSV 读取行作为数据记录。我遇到的问题是该程序在 Eclipse 控制台中完美运行,但是当我尝试使用 Java 编译器从 cmd 运行问题时,我的代码指定 CSV 文件的位置时不起作用,它在那里显示一个未知错误。我已经尝试了一切,例如将文件移动到另一个包中或使用其他一些代码来读取文件我得到了相同的结果,这在 eclipse 中它可以工作,但在实际的 cmd 中它不起作用。下面是错误和读取文件的方法。
java.io.FileNotFoundException: src\PostCodeENW.csv (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at Java.io.FileReader.<init>(Unknown Source)
public void loadfile() {
System.out.println(System.getProperty("user.dir"));
String csvFile = "src/PostcodeENW.csv";
BufferedReader br = null;
String line = "";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] ar = line.split("/");
PostCode = ar[0].trim().replaceAll(" ", " ");
String[] ar1 = PostCode.split("\\s");
String PostCodeP1 = ar1[0];
String PostCodeP2 = ar1[1];
int Easting = Integer.parseInt(ar[1]);
int Northing = Integer.parseInt(ar[2]);
String Ward = ar[3];
Location geo = new Location(PostCodeP1, PostCodeP2, Easting, Northing, Ward);
map.put(geo.getkey(), geo.getValue());
// System.out.println(PostCodeP1 + " " + PostCodeP2 + " " + Easting + " " + Northing + " " + Ward);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done Adding all records");
System.out.println("Database currently holds: " + map.size()); //Test how much elements are currently in the HashMap
}
【问题讨论】:
标签: java cmd bufferedreader filereader