怪异的线程“主” java.io.FileNotFoundException I / O Java [英] Weird exception in thread "main" java.io.FileNotFoundException I/O Java
问题描述
线程main中的异常java.io. FileNotFoundException:\src\product.txt(没有这样的文件或目录)
在java.io.FileInputStream.open(Native方法)$ b $在java.io.FileInputStream。< init>(FileInputStream .java:120)
at dao.Inventory.readFile(Inventory.java:30)
at view.InventoryView.init(InventoryView.java:33)
at view.InventoryView。< (InventoryView.java:21)
at view.InventoryView.main(InventoryView.java:211)
但是,我的src文件夹中有product.txt文件。
我的代码如下:
public void readFile()throws IOException {
//输入文件必须在第一个参数中提供
InputStream istream;
File inputFile = new File(\\src\\product.txt);
istream = new FileInputStream(inputFile);
BufferedReader lineReader;
lineReader = new BufferedReader(new InputStreamReader(istream));
字符串行; ((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());
$ / code $ / pre
我认为我的代码没有任何问题。另外,我看到一个类似FILE.TXT.TXT的隐藏扩展的帖子,你将如何在MacOSX中显示一个隐藏的扩展?有什么建议么? (除了隐藏的扩展问题之外,还会有其他问题吗?)
解决方案 / src / product。 txt
是一个绝对路径,所以程序会尝试在根路径(/)的src文件夹中找到该文件。使用 src / product.txt
,程序将使用它作为相对路径。
I have this error when I am trying to read the file:
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)
But the thing is, I have the product.txt in my src folder.
My code is the following:
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());
}
}
I don't think anything is wrong with my code. Also, I saw a similar post about a hidden extension like FILE.TXT.TXT, how would you show a hidden extension in MacOSX?? Any suggestions? (Would there be any other problem besides the hidden extension issue?)
解决方案 /src/product.txt
is an absolute path, so the program will try to find the file in the src folder of your root path (/). Use src/product.txt
so the program will use this as a relative path.
这篇关于怪异的线程“主” java.io.FileNotFoundException I / O Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!