有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

FileReaderJava。木卫一。指定目录时FileNotFoundException

我试图用一个名为“helloworld”的txt文件在Java中定义一个文件。我已将此文件放置在资源文件夹中,在创建文件时,我将其定义为:

File file = new File("/helloworld");

但是,我在编译时遇到了这个错误

 Exception in thread "main" java.io.FileNotFoundException: /helloworld 
    (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileReader.<init>(FileReader.java:72)
    at Tests.main(Tests.java:15)

这是我试图执行的全部代码,如果这有助于解决此问题

// Java Program to illustrate reading from FileReader
// using BufferedReader
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
public class Tests
{
  public static void main(String[] args)throws Exception
  {


      File file = new File("/helloworld");

      BufferedReader br = new BufferedReader(new FileReader(file));

      String st;
      while ((st = br.readLine()) != null)
        System.out.println(st);
  }
}

谢谢你的帮助


共 (4) 个答案

  1. # 1 楼答案

    诊断起来很容易:您指定的路径以斜杠开始,因此这意味着文件应该位于文件系统的根目录中。最好去掉前导斜杠,然后:

    • 或者在文件所在的目录下启动程序
    • 在实例化File对象时,在代码中指定绝对/相对路径
  2. # 2 楼答案

    如果文件位于resources文件夹中,并且打算与程序捆绑在一起,则需要将其视为资源,而不是文件

    这意味着您不应该使用File类。您应该使用Class.getResourceClass.getResourceAsStream方法读取数据:

    BufferedReader br = new BufferedReader(
        new InputStreamReader(
            Tests.class.getResourceAsStream("/helloworld")));
    

    如果您想将程序作为一个组件分发,这一点就变得尤为重要。jar文件。A.jar文件是一个压缩的归档文件(实际上是一个具有不同扩展名的zip文件),它包含编译的类文件和资源。因为它们都被压缩成一个。jar文件,它们根本不是单独的文件,因此file类无法引用它们

    尽管File类对您尝试执行的操作没有帮助,但您可能需要研究绝对文件名相对文件名的概念通过以/开头的文件名,您指定了一个绝对文件名,这意味着您告诉程序在一个特定的位置查找该文件—一个几乎肯定不会驻留该文件的位置

  3. # 3 楼答案

    public File​(String pathname)
    

    Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

    您正试图创建一个新的File实例,但找不到名为helloworld的文件,或者由于其他原因。这就是为什么你会出错

     Exception in thread "main" java.io.FileNotFoundException: /helloworld
    
    1. 命名文件不存在
    2. 命名文件实际上是一个目录
    3. 由于某些原因,无法打开命名文件进行读取

    您说您试图定义一个文件,但您的代码似乎是可读的。如果要创建文件,请尝试下面的一个

    import java.io.*;
    import java.nio.charset.StandardCharsets;
    
    
    class TestDir {
        public static void main(String[] args) {
            String fileName = "filename.txt";
    
            try (Writer writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileName), StandardCharsets.UTF_8))) {
                writer.write("write something in text file");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
  4. # 4 楼答案

    尝试下面的方法来了解程序正在查找的文件夹或文件路径在哪里

    System.out.println(file.getAbsolutePath());
    

    File file = new File("/helloworld");
    

    我认为您的程序正在查找c:\helloworld,并且您的C驱动器中没有文件或文件夹的名称为helloword

    如果将helloword.txt放入C驱动器,并且

    File file = new File("C:\\helloworld.txt");
    

    FileNotFoundException将消失