简体   繁体   中英

Java.io.FileNotFoundException when specifying a directory

I'm trying to define a File in Java with a txt file called "helloworld". I've placed this file in a resources folder and when making the file I defined it like this:

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

However I get this error when compiling

 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)

This is the entire code I am trying to execute if that helps troubleshoot this issue

// 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);
  }
}

Thank you for the help!

 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.

You are trying to create a new File instance but the file called helloworld is not found or some other reasons. That's why you get the error,

 Exception in thread "main" java.io.FileNotFoundException: /helloworld
  1. The named file does not exist.
  2. The named file is actually a directory.
  3. The named file cannot be opened for reading for some reason.

You say that you try to define a file but your code seems to read. Try below one if you want to create a file ,

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();
        }
    }
}

It is easy to diagnose: The path you specified starts with a slash, so it means that the file is expected to be located at the root directory of the filesystem. You'd better strip off the leading slash, and:

  • Either start your program at the same directory the file is at.
  • Either specify an absolute/relative path in your code when instantiating the File object.

If the file is in a resources folder and is intended to be bundled with your program, you need to treat it like a resource, not a file.

This means you should not use the File class. You should read your data with the Class.getResource or Class.getResourceAsStream method:

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

This becomes especially important if you want to distribute a program as a .jar file. A .jar file is a compressed archive (actually a zip file with different extension) which contains both compiled class files and resources. Since they are all compressed into one .jar file, they are not individual files at all, so there is no way the File class can refer to them.

Although the File class is not useful for what you're trying to do, you may want to research the concept of absolute file names and relative file names. By starting a file name with / , you are specifying an absolute file name, which means you are telling the program to look for the file in a specific place—a place where the file almost certain will not reside.

Try bellow to know where is the folder or file's path, which your program is looking for

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

With

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

I think your program is looking for c:\\helloworld , and there is no file or folder's name is helloword in your C drive

If you put the helloword.txt into C drive, and

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

FileNotFoundException will disappear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM