线程“main”中的奇怪异常 java.io.FileNotFoundException I/O Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9953328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Weird exception in thread "main" java.io.FileNotFoundException I/O Java
提问by ShaunK
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.
但问题是,我的 src 文件夹中有 product.txt。
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?)
我认为我的代码没有任何问题。另外,我看到了一个类似的帖子,关于像 FILE.TXT.TXT 这样的隐藏扩展名,你会如何在 MacOSX 中显示隐藏扩展名??有什么建议?(除了隐藏扩展问题,还会有其他问题吗?)
回答by Luiggi Mendoza
/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.
/src/product.txt
是绝对路径,因此程序会尝试在您的根路径 (/) 的 src 文件夹中查找该文件。使用src/product.txt
所以程序将使用它作为相对路径。
回答by James
It's possible (most likely?) that your Java code is not executing inside the parent folder of src, but instead inside a 'class' or a 'bin' folder with the compiled java .class files.
有可能(最有可能?)您的 Java 代码不是在 src 的父文件夹中执行,而是在带有编译的 java .class 文件的“class”或“bin”文件夹中。
Assuming that 'src' and 'bin' are in the same directory, you could try ..\\src\\product.txt
假设 'src' 和 'bin' 在同一目录中,您可以尝试 ..\\src\\product.txt
回答by Mike Adler
As other commenters stated, the path is absolute and points to \src\product.txt which is (hopefully) not where your sources are stored.
The path separator should be set in an OS-independent manner using the System.getProperty("path.separator") property. On a Unix system, you'll have trouble with hard coded backslashes as path separators. Keep it portable!
正如其他评论者所说,路径是绝对的,指向 \src\product.txt,它(希望)不是您的源存储的位置。
应该使用 System.getProperty("path.separator") 属性以独立于操作系统的方式设置路径分隔符。在 Unix 系统上,使用硬编码的反斜杠作为路径分隔符会遇到麻烦。保持便携!
String pathSeparator = System.getProperty("path.separator"); String filePath = "." + pathSeparator + "src" + pathSeparator + "product.txt"; File file = new File(filePath);
String pathSeparator = System.getProperty("path.separator"); String filePath = "." + pathSeparator + "src" + pathSeparator + "product.txt"; File file = new File(filePath);
or better yet:
或者更好:
// this could reside in a non-instantiable helper class somewhere in your project
public static String getRelativePath(String... pathElements) {
StringBuilder builder = new StringBuilder(".");
for (String pathElement : pathElements) {
builder.append(System.getProperty("path.separator");
builder.append(pathElement);
}
return builder.toString();
}
// this is where your code needs a path
...
new File(getRelativePath("src", "product.txt");
...