【问题标题】:Weird exception in thread "main" java.io.FileNotFoundException I/O Java线程“主”java.io.FileNotFoundException I/O Java 中的奇怪异常
【发布时间】: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


【解决方案1】:

/src/product.txt 是绝对路径,所以程序会尝试在你的根路径(/)的 src 文件夹中查找文件。使用src/product.txt,这样程序就会使用它作为相对路径。

【讨论】:

  • +1 您应该使用 \\ 而不是 \,或者更好的是,使用 / 独立操作系统。
  • 我是java新手,面临同样的问题,如果目标文件不是src怎么办?我尝试了所有这些,但仍然
  • @user3188912 然后使用文件的绝对路径。如果您在 Windows 上,那么它将是“C:\\path\\to\\your\\file.ext”,如果您在 *-nix 系统上,那么它将是“/path/to/your /file.ext"。
【解决方案2】:

有可能(很可能?)您的 Java 代码没有在 src 的父文件夹中执行,而是在包含已编译的 java .class 文件的“class”或“bin”文件夹中执行。

假设'src'和'bin'在同一个目录下,你可以试试..\\src\\product.txt

另见http://en.wikipedia.org/wiki/Path_(computing)

【讨论】:

    【解决方案3】:
    1. 正如其他评论者所说,路径是绝对的,并且指向 \src\product.txt (希望)不在哪里 您的来源已存储。

    2. 路径分隔符应使用独立于操作系统的方式设置 System.getProperty("path.separator") 属性。在 Unix 系统上,将硬编码的反斜杠作为路径分隔符会遇到麻烦。保持便携!

    String pathSeparator = System.getProperty("path.separator");
    String filePath = "." + pathSeparator + "src" + pathSeparator + "product.txt";
    File file = new File(filePath);
    

    或者更好:

    // 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");
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多