线程"main"中的奇怪异常java.io.FileNotFoundException I/O Java

Sha*_*unK 3 java file-io filenotfoundexception

我在尝试读取文件时遇到此错误:

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)
Run Code Online (Sandbox Code Playgroud)

但问题是,我的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());
            }
        }
Run Code Online (Sandbox Code Playgroud)

我认为我的代码没有任何问题.另外,我看到一篇关于像FILE.TXT.TXT这样的隐藏扩展的类似帖子,你如何在MacOSX中显示一个隐藏的扩展?有什么建议?(除隐藏的扩展问题外还会有其他问题吗?)

Lui*_*oza 6

/src/product.txt是一个绝对路径,因此程序将尝试在根路径(/)的src文件夹中找到该文件.使用src/product.txt这样程序将使用它作为相对路径.