Java Cannot find file?

Hi I am trying to set up a scanner to print out contents of a text file. Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {

    public static void main(String[] args) {

        // Location of file to read
        File file = new File("CardNative.java.txt");

        try 
        {

                Scanner scanner = new Scanner(file);

                while (scanner.hasNextLine()) 
                {
                    String line = scanner.nextLine();
                    System.out.println(line);
                }
                scanner.close();
        } 
        catch (FileNotFoundException e) 
        {
              e.printStackTrace();
        }

    }
}

I have created a source folder in the project and put the text file in there. However I keep getting this error:

java.io.FileNotFoundException: CardNative.java.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.util.Scanner.<init>(Scanner.java:636)
    at ScannerReadFile.main(ScannerReadFile.java:14)

You can use use System.out.println(System.getProperty("user.dir")); to see on what folder Java is looking by default for the file. If the file doesn't exist there you have to specify the entire path to the file.

Java can't find my file, Java can't find my file. I wrote this program that reads in lines from a csv file. When I run the code, I get an error message below. The CSV file is� I ran your code as is and it compiled without errors. Right-click the .csv file in the IntelliJ project Window and select "Copy Path". This will give you the absolute path of the file which you can then paste as the argument for your function

This should work.

public static void main(String[] args) {


            Scanner scanner = new Scanner(ScannerReadFile.class.getResourceAsStream("CardNative.java.txt"));

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();

    }
}

Can't find file?, Hi, I'm trying to read lines from a text file using file and a scanner objects. According to my Java book this should be right, but it doesn't work. I' java.io.FileNotFoundException: data.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) Cannot find file for FileReader. By Spooge in forum New To Java Replies: 11 Last Post: 10-20-2010, 11:12 PM. JBOSS Depl

You need to make sure that your file is placed in the same directory from where you're running your program. Try adding this at the top of your main function, to see what's the current directory, and if your file is actually in that directory:

System.out.println(System.getProperty("user.dir"));

Program not finding file in same folder [Solved] (Beginning Java , Below it I am outputting each count just to see if it's working (except FileNotFoundException: midterm.txt (The system cannot find the file� I have started learning java a few days earlier. I was doing good so far except I git the error: java.io.FileNotFoundException: (The system cannot find the file specified) I was trying to read a file from my system as an assignment but it’s not working in my case. I have got a very simple set code and I am attaching my code snippet below

new File(String pathname);

The coinstructor you are using takes filepath as an argument, absolute or relative. If it is relative, it will be the execute path/your_string. So you should put the file to the same folder as the compiled .jar file.

File file1 = new File("text.txt");
File file2 = new File("D:/documents/test.txt");

If the programm is executing from C:/programms/myprj.jar, so file1 will open "C:/programms/test.txt" and file2 will open "D:/documents/test.txt" independently of the executing path.

http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)

This file exists,but Java can't find it., Just the tinyest clue about your code would help reduce the number of possible causes to a finite number. If you are using the standard file open dialog then it's� If you are using the standard file open dialog then it's just a bug in your following code. If you are entering the file path/name as a string then it's probably looking in a directory that you don't expect. You can check that out by writing a few bytes to the same file and seeing where it appears in your file system. harinath_2007 56

I was posting my answer in comment but I am not allowed to comment because I have no enough reputations. As in your comment, you are using back slashes in the file path. Instead use double back slashes \ OR one forward /. eg C:/java/file.txt You should provide it the right and actual path of the file OR make sure that the file is lying there where your source is.

public class ScannerReadFile {

    public static void main(String[] args) {

        // Location of file to read
        File file = new File("C:/Users/EastCorporation/Desktop/CardNative.java.txt");

        try {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

How to Avoid the Java FileNotFoundException When Loading , Java programs cannot easily load files once they have been Let's see how Java allows us to access resource files after our code has been� Exception in thread "main" java.io.FileNotFoundException: midterm.txt (The system cannot find the file specified) at java.io.FileInputStream.open0 (Native Method) at java.io.FileInputStream.open (Unknown Source) at java.io.FileInputStream.<init> (Unknown Source)

Finding Files (The Java™ Tutorials > Essential Classes > Basic I/O), See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. Finding Files. If you have� The compiler is not able to find any .java file that is outside the bin directory. I have JVM installed in C:\jdk1.3\bin> I have JVM installed in C:\jdk1.3\bin> and I want to keep all my .java files in C:\jdk1.3\bin\samples> Could you please tell me what exactly should I mention in the classpath and path..?

Java - Read a file from resources folder, Java - Read a file from resources folder. File; import java.io. testClassLoader\ target\classes\file\test.txt (The system cannot find the path� ------ INVALID JSON FILE TO BOOKS ------ [EXPECTED] java.io.FileNotFoundException: invalid.json (The system cannot find the file specified) The Airbrake-Java library provides real-time error monitoring and automatic exception reporting for all your Java-based projects.

FileReader cannot find a file in the same directory Java question , It's also just a plain text file made with notepad. "Exception in thread "main" java. io.FileNotFoundException: mazefile.txt (The system cannot find� Find the Java Control Panel - Versions below 7u40 Windows 10 Right-click on the Start button and select the Control Panel option. In the Windows Control Panel, click on Programs.

Comments
  • I suspect, you only need to write CardNative.java
  • Is the file you're trying to load on the classpath?
  • no it is actually a text file, the full name is CardNative.java.txt which is a bit awkward sounding but the full name
  • as the imrichardcole, checks the file is exist. if it is then checks the extension of it? If you can give us the File name with extension, we can try to help. Thanks
  • sorry, I assume it exists, but is the file on the classpath?
  • Hi this was hhelpful, and I found the path to be: c:\Users\Mara\workspace\ReadFile\RSource\CardNative.java.txt but when I put File file = new File("c:\Users\Mara\workspace\ReadFile\RSource\CardNative.java.txt"); the c:\ is underlined as having an error: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\
  • You need to change the "\" to "\\" in your path
  • Thanks sorted the problem!
  • I found the file path to be c:\Users\Mara\workspace\ReadFile\RSource\CardNative.java.txt but when I put File file = new File("c:\Users\Mara\workspace\ReadFile\RSource\CardNative.java.txt"); the c:\ is underlined as having an error: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\
  • the c:/ is now underlined as having an error: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )