【发布时间】:2012-03-31 05:39:10
【问题描述】:
当我尝试读取文件时出现此错误:
Exception in thread "main" java.io.FileNotFoundException: \src\product.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at dao.Inventory.readFile(Inventory.java:30)
at view.InventoryView.init(InventoryView.java:33)
at view.InventoryView.<init>(InventoryView.java:21)
at view.InventoryView.main(InventoryView.java:211)
但问题是,我的 src 文件夹中有 product.txt。
我的代码如下:
public void readFile() throws IOException {
// input file must be supplied in the first argument
InputStream istream;
File inputFile = new File("\\src\\product.txt");
istream = new FileInputStream(inputFile);
BufferedReader lineReader;
lineReader = new BufferedReader(new InputStreamReader(istream));
String line;
while ((line = lineReader.readLine()) != null) {
StringTokenizer tokens = new StringTokenizer(line, "\t");
// String tmp = tokens.nextToken();
// System.out.println("token " + tmp);
ActionProduct p = new ActionProduct();
prodlist.add(p);
String category = p.getCategory();
category = tokens.nextToken();
System.out.println("got category " +category);
int item = p.getItem();
item = Integer.parseInt(tokens.nextToken());
String name = p.getName();
System.out.println("got name " +name);
double price = p.getPrice();
price = Double.parseDouble(tokens.nextToken());
int units = p.getUnits();
units = Integer.parseInt(tokens.nextToken());
}
}
我认为我的代码没有任何问题。另外,我看到了一个类似的帖子,关于像 FILE.TXT.TXT 这样的隐藏扩展,你将如何在 MacOSX 中显示隐藏扩展?有什么建议? (除了隐藏扩展问题,会不会有其他问题?)
【问题讨论】:
-
如果您的文件位于
src文件夹中,您应该将src\\product.txt作为路径。 -
改用
File inputFile = new File("./src/product.txt");。 -
或文件 inputFile = new File("src\\product.txt");
标签: java file-io filenotfoundexception