java.io.FileNotFoundException: the system cannot find the file specified

前端 未结 8 1420
Happy的楠姐
Happy的楠姐 2020-11-22 15:27

I have a file named \"word.txt\".

It is in the same directory as my java file.

But when I try to access it in the following code th

相关标签:
8条回答
  • 2020-11-22 15:50

    Try to create a file using the code, so you will get to know the path of the file where the system create

    File test=new File("check.txt");
    if (test.createNewFile()) {
        System.out.println("File created: " + test.getName());
      }
    
    0 讨论(0)
  • 2020-11-22 15:57

    Relative paths can be used, but they can be tricky. The best solution is to know where your files are being saved, that is, print the folder:

    import java.io.File;
    import java.util.*;
    
    public class Hangman1 {
    
        public static void main(String[] args) throws Exception {
            File myFile = new File("word.txt");
            System.out.println("Attempting to read from file in: "+myFile.getCanonicalPath());
    
            Scanner input = new Scanner(myFile);
            String in = "";
            in = input.nextLine();
        }
    
    }
    

    This code should print the folder where it is looking for. Place the file there and you'll be good to go.

    0 讨论(0)
  • 2020-11-22 16:00

    Make sure when you create a txt file you don't type in the name "name.txt", just type in "name". If you type "name.txt" Eclipse will see it as "name.txt.txt". This solved it for me. Also save the file in the src folder, not the folder were the .java resides, one folder up.

    0 讨论(0)
  • 2020-11-22 16:02

    I have the same problem, but you know why? because I didn't put .txt in the end of my File and so it was File not a textFile, you shoud do just two things:

    1. Put your Text File in the Root Directory (e.x if you have a project called HelloWorld, just right-click on the HelloWorld file in the package Directory and create File
    2. Save as that File with any name that you want but with a .txt in the end of that I guess your problem is solved, but I write it to other peoples know that. Thanks.
    0 讨论(0)
  • 2020-11-22 16:05

    Put the word.txt directly as a child of the project root folder and a peer of src

    Project_Root
        src
        word.txt
    

    Disclaimer: I'd like to explain why this works for this particular case and why it may not work for others.

    Why it works:

    When you use File or any of the other FileXxx variants, you are looking for a file on the file system relative to the "working directory". The working directory, can be described as this:

    When you run from the command line

    C:\EclipseWorkspace\ProjectRoot\bin > java com.mypackage.Hangman1

    the working directory is C:\EclipseWorkspace\ProjectRoot\bin. With your IDE (at least all the ones I've worked with), the working directory is the ProjectRoot. So when the file is in the ProjectRoot, then using just the file name as the relative path is valid, because it is at the root of the working directory.

    Similarly, if this was your project structure ProjectRoot\src\word.txt, then the path "src/word.txt" would be valid.

    Why it May not Work

    For one, the working directory could always change. For instance, running the code from the command line like in the example above, the working directory is the bin. So in this case it will fail, as there is not bin\word.txt

    Secondly, if you were to export this project into a jar, and the file was configured to be included in the jar, it would also fail, as the path will no longer be valid either.

    That being said, you need to determine if the file is to be an embedded-resource (or just "resource" - terms which sometimes I'll use interchangeably). If so, then you will want to build the file into the classpath, and access it via an URL. First thing you would need to do (in this particular) case is make sure that the file get built into the classpath. With the file in the project root, you must configure the build to include the file. But if you put the file in the src or in some directory below, then the default build should put it into the class path.

    You can access classpath resource in a number of ways. You can make use of the Class class, which has getResourceXxx method, from which you use to obtain classpath resources.

    For example, if you changed your project structure to ProjectRoot\src\resources\word.txt, you could use this:

    InputStream is = Hangman1.class.getResourceAsStream("/resources/word.txt");
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    

    getResourceAsStream returns an InputStream, but obtains an URL under the hood. Alternatively, you could get an URL if that's what you need. getResource() will return an URL

    For Maven users, where the directory structure is like src/main/resources, the contents of the resources folder is put at the root of the classpath. So if you have a file in there, then you would only use getResourceAsStream("/thefile.txt")

    0 讨论(0)
  • 2020-11-22 16:14

    Your file should directly be under the project folder, and not inside any other sub-folder.

    If the folder of your project is named for e.g. AProject, it should be in the same place as your src folder.

    Aproject
            src
            word.txt
    
    0 讨论(0)
提交回复
热议问题