I am attempting to take input from a file called gift1.in, however I keep getting a file not found error. Any ideas?

            Asked
            
        
        
            Active
            
        
            Viewed 1,500 times
        
    0
            
            
         
    
    
        Dude156
        
- 158
- 1
- 12
- 
                    where is this `gift1.in` file located? is it on the project root? `FileNotFoundException` can be caused by several cases. Please take a look on [previous QA](https://stackoverflow.com/a/19309163/4648586). – Bagus Tesa Jan 14 '19 at 00:52
- 
                    Yep, located in USACO folder (project root) – Dude156 Jan 14 '19 at 00:53
- 
                    check on your project folder, is there folder named `target` (or any folder that contains the compiled `jar` file), copy the `gift1.in` into that folder and try to run again. i think it just path issue. – Bagus Tesa Jan 14 '19 at 00:55
- 
                    Ah, the only folders in the project root appear to be bin and src. I put the gift1.in in this directory. Darn, I can't figure this out :( – Dude156 Jan 14 '19 at 00:57
- 
                    1Try calling `f.getAbsolutePath()` right after you instantiate it so that you have a better idea of where it thinks it is. Then check your Run Configuration for what its working directory is. – nitind Jan 14 '19 at 01:20
- 
                    1There is nothing wrong with where your file is. (At least not necessarily) Eclipse requires you to handle the `fileNotFoundException`, and will not compile unless you have a `throws` deceleration or surround it in a try catch. It has nothing to do with the file itself, just the fact that you are doing io operations and eclipse requires you to handle some of the errors – GBlodgett Jan 14 '19 at 03:22
- 
                    @GBlodgett Ah, Java is cheeky that way... Thanks! – Dude156 Jan 14 '19 at 19:10
2 Answers
0
            The error in the screenshot is a unhandled exception compiler error.
FileNotFoundException is a checked exception since it derives from IOException.
A checked exception will always generate a compiler error when compiled if not caught or thrown.
Either add throws FileNotFoundException in main or put the code throwing it in a try/catch block.
public static void main(String args[]) throws FileNotFoundException
or
Scanner input = null;
try {
  input = new Scanner(f);
}
catch {
  System.Out.Print("File not found");
  return 1;
}
The file also needs to be in a resource folder for the build script to copy to the target folder to run.
I am not sure about Eclipse, but the standard I have seen is for the resource folder to be located  in src/main/resources.
 
    
    
        Richard
        
- 154
- 1
- 7
- 
                    Yes, but why would the file not be found? I put it in the project root. Excuse me if I'm misunderstanding. – Dude156 Jan 14 '19 at 01:01
0
            
            
        I dont have eclipse right now but the problem might be the path. Try this
File f = new File(System.getProperty("user.dir") + "gift1.in");
or
File f = new File("[/absolute/path/to/file]");
 
    
    
        Talha Akber
        
- 56
- 8