0

I'm getting the error Exception in thread "main" java.nio.file.NoSuchFileException but I'm sure the file exists at the given location C:\\Users\\Admin\\Desktop\\Java.txt".

Why do I still get this error?

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;

public class ReadData {
  public static void main(String[] args) throws IOException {
        
    Scanner file = new Scanner(Paths.get("C:\\Users\\Admin\\Desktop\\Java.txt", "UTF-8"));
    int int_value;
    while ((file.hasNextInt())) {
        int_value = file.nextInt();
        System.out.println("Data:" + int_value);
    }

    file.close();
  }
}
Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
Nagai Kei
  • 23
  • 1
  • 1
  • 6
  • 1
    "no such file" error suggests that the file you're trying to use does not exist. Do you think it does? – Joni Jul 05 '20 at 02:28
  • It occurs both VSC and Elipse IDE, And I just downloaded it yesterday. How to configed it – Nagai Kei Jul 05 '20 at 02:34
  • Umm ... look in the file system to see if there is a file at "C:\\Users\\Admin\\Desktop\\Java.txt". If it doesn't exist **at that location** ... that's why your application cannot find it! Possible solutions: 1) create the file, or 2) correct the path in your program. – Stephen C Jul 05 '20 at 02:44
  • 1
    I have checker, And I have inserted the exactly address file beforewards. But still it error – Nagai Kei Jul 05 '20 at 02:59
  • OK, I mistaken a problem that I cannot explain by word. Anyway Thank you very much – Nagai Kei Jul 05 '20 at 03:03
  • _OK, I mistaken a problem that I cannot explain by word. Anyway Thank you very much_ – edited your question (pending approval) to better describe your problem. Also see @DevilsHnd answer as it explains why you get this error even though your file path is correct. And don't forget to accept (and up-vote) an answer when it answers your question. – Ivo Mori Jul 05 '20 at 08:38
  • Well remove the "UTF-8" if you dont have a file named "UTF-8": Scanner file =new Scanner(Paths.get("C:\\Users\\Admin\\Desktop\\Java.txt")); – kai Jul 05 '20 at 09:37
  • Paths.get("C:\\Users\\Admin\\Desktop\\Java.txt", "UTF-8") means a file with path "C:\\Users\\Admin\\Desktop\\Java.txt"\\UTF-8". Is that what you wanted? – armagedescu Jul 05 '20 at 17:39

2 Answers2

4

I believe your issue is with your Paths.get() method:

Scanner file = new Scanner(Paths.get("C:\\Users\\Admin\\Desktop\\Java.txt", "UTF-8"));

The closing parentheses for the Paths.get() method is in the wrong place. What you are actually supplying to the Scanner object (or what the get() method interprets it to be) is a path like this:

"C:\Users\Admin\Desktop\Java.txt\UTF-8"

That particular path obviously can not be found. It should be:

Scanner file = new Scanner(Paths.get("C:\\Users\\Admin\\Desktop\\Java.txt"), "UTF-8");

You may want to also consider utilizing the Try With Resources mechanism. It will auto-close the file reader:

try (Scanner file = new Scanner(Paths.get("C:\\Users\\Admin\\Desktop\\Java.txt"), "UTF-8")) {
    int int_value;
    while ((file.hasNextInt())) {
        int_value = file.nextInt();
        System.out.println("Data:" + int_value);
    }
}
catch (IOException ex) {
    ex.printStackTrace();
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
0

just keep the text file in your project folder and simply change the code like below

Scanner file=new Scanner(new File("Java.txt")); hope it will solve your issue

Mahamudul Hasan
  • 2,745
  • 2
  • 17
  • 26