Click here to Skip to main content
14,717,158 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm new to Java and I come from a C/C++ background. I'm having an issue reading in a file using the Scanner class. I keep getting the FileNotFoundException and I have no idea what I am doing wrong. input.txt is in the same folder as Main.java yet the Scanner still throws the exception. My code is displayed below:


import java.io.File;
import java.nio.file.Path;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        File file = new File("C:/Users/josep/OneDrive/Documents/java/input.txt/");
       //File file = new File("input.txt"); //I don't know why this doesn't work since the file is in the same folder as Main.java

        if( file.exists() )
        {
            System.out.println("File exists");
        }
        else
        {
            System.out.println("Doesn't exist");
        }
    
        Scanner input = new Scanner(file);

        //String str = input.nextLine();
        //System.out.println("Str: " + str);

        //input.close();

    }
}



I used file.exists() to see if the file exists and it returns true. If file.exists() returns true, I don't understand how Scanner is unable to find the file. I 've tried a bunch of stuff to try and fix it but nothing I've tried works. Whenever I run it, I get the output below:


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        Unhandled exception type FileNotFoundException

        at Main.main(Main.java:21)



Does anyone have any idea what could be the issue? Any help would be greatly appreciated.

________________________________________________________________________________________________________

Also, why do I have to specify the path instead of just putting "input.txt" as in:



File file = new File("C:/Users/josep/OneDrive/Documents/java/input.txt/");
File file = new File("input.txt"); //why won't this work?



when I try the second one (new File("input.txt")), file.exists() returns false. I don't understand why this is since input.txt is in the same folder as Main.java. In C++ you'd only have to put the file name if the .cpp file and the input file are in the same folder, so I don't understand why that is not the case in Java, or am I doing something wrong (I probably am doing something wrong)?

What I have tried:

The above code is mostly what I have tried. Anything else I tried were more of random trial and error.
Posted
Updated 17-Dec-20 1:35am
Comments
Sandeep Mewara 17-Dec-20 2:49am
   
Can you try:File file = new File("C:/Users/josep/OneDrive/Documents/java/input.txt");

Slash removed at the end. Quick view, rest all looks okay. Assuming, access is there. Quick check would be to put the url in browser and see you get back what you seek.
Richard MacCutchan 17-Dec-20 6:35am
   
All due to OP not reading error messages properly.

This path:
"C:/Users/josep/OneDrive/Documents/java/input.txt/"
ends in a slash - so it's saying "this is a directory, not a file" - so the system looks for a directory of that name and fails to find it.
Remove the slash at the right hand end, and it should work.
   
Comments
Richard MacCutchan 17-Dec-20 6:35am
   
See below.
Member 14767000 17-Dec-20 10:22am
   
The slash was actually not the issue. I had just put the slash to see if it'd work but it still did not. The issue was that I did not put a try catch block or "throws FileNotFoundException". Thank you for your input!
I have tried both versions and both work as expected. The problem that you are seeing is not that the file does not exist, but that you are not checking for an exception which the Scanner class may throw. Just change your code to the following:
try
{
    Scanner input = new Scanner(file);
}
catch (FileNotFoundException ff)
{
    System.out.println("Exception " + ff.toString());
}

If the file exists that will proceed as expected.
   
Comments
Member 14767000 17-Dec-20 10:36am
   
I put "throws FileNotFoundException" and now it works, thank you! However, both the "input.txt" and "Main.java" are in the same folder so why can't the file be found if I instead put:< File file = new File("input.txt"); >. When I use file.exists() to check if it exists, it returns false in this case. Why can't File find the file?



public static void main(String[] args) throws FileNotFoundException
{
// File file = new File("C:/Users/josep/OneDrive/Documents/java/input.txt/");
File file = new File("input.txt"); //why won't this work?

if( file.exists() )
{
System.out.println("File exists");
}
else
{
System.out.println("Doesn't exist"); //This prints out.
}
//System.out.println(file.getAbsolutePath());
Scanner input = new Scanner(file);

String str = input.nextLine();
System.out.println("Str: " + str);

input.close();

}
Richard MacCutchan 17-Dec-20 10:52am
   
I just built that exact code and it works fine. You must be doing something different. Are you buildin in some framework or something other than ordinary Java?
Richard MacCutchan 17-Dec-20 11:07am
   
Add these two lines to your code:
System.out.println("Current Directory = " + System.getProperty("user.dir"));
System.out.println(file.getCanonicalPath());

which may help to find out what is happening.
Richard MacCutchan 17-Dec-20 11:12am
   
OK, I missed something here. Although Main.java and input.txt are in the same folder, that is not necessarily where the Java class file (the executable code) is being placed. So you the answers to my previous two questions may help to find out where it is.
Member 14767000 17-Dec-20 14:37pm
   
Thank you so much for the help, I've figured out where to put the file and now everything works!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900
欧美日韩亚洲中字二区_欧美日韩在线无码一区二区三区_欧美日韩无线码免费