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

前端 未结 8 1566
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: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.

提交回复
热议问题