Oracle教程Java基础I/O程序抛FileNotFoundException,Eclipse中求助排查
Hey there! Let's figure out why your CopyBytes program is throwing that frustrating FileNotFoundException, even when you've tried placing the file in the working directory and using an absolute path. Below are the most common culprits specific to Eclipse, along with fixes for each:
First, Let's Clarify the Code Context
I’m assuming your full code looks something like this (based on Oracle’s tutorial snippet you shared):
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String[] args) throws IOException { // Hardcoded file path example (adjust based on your actual code) FileInputStream in = new FileInputStream("input.txt"); FileOutputStream out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); } }
Common Causes & Fixes
Eclipse’s working directory isn’t where you think it is
This is the #1 reason for this issue in Eclipse. By default, some versions set the working directory to thebinfolder of your project (where compiled.classfiles live), not the project root where you’re placing your input file.
Fix:- Add this line to your
mainmethod to print the actual working directory:System.out.println("Current working directory: " + System.getProperty("user.dir")); - Place your input file in the directory printed, or change Eclipse’s working directory:
- Go to
Run > Run Configurations - Select your
CopyBytesprogram under Java Applications - Switch to the
Argumentstab - Under "Working Directory", select "Other" and click "Workspace..." to choose your project root folder.
- Go to
- Add this line to your
Filename typos or hidden whitespace
It’s easy to miss a typo (e.g.,input.txtvsinput1.txt) or invisible whitespace at the end of the filename (like a space you can’t see). Even on Windows (which is case-insensitive), sometimes Java’s file handling can be strict if the filename has unexpected characters.
Fix: Double-check the filename in your code matches exactly what’s on your filesystem. If you’re using an absolute path, copy-paste the path from File Explorer to avoid typos.File permissions or locks
If the file is open in another program (like Notepad, a PDF reader, or antivirus software), Eclipse/Java might not be able to access it. Or, if you’re using an absolute path to a restricted directory (likeC:\Windowsor a system folder), your user account might not have read permissions.
Fix: Close any programs that might be using the file, and try moving the file to a non-restricted folder (like your Documents or project root).Case sensitivity issues (for non-Windows systems)
If you’re running Eclipse on Linux or macOS, filenames are case-sensitive. For example,Input.txtis not the same asinput.txt, even if they look similar.
Fix: Ensure the case of the filename in your code matches exactly with the actual file on your system.Missing program arguments (if applicable)
If your program is supposed to take the filename as a command-line argument (viaargs[0]), but you haven’t set those arguments in Eclipse’s run configuration, it’ll try to read a non-existent file.
Fix: Go toRun > Run Configurations > Arguments, and add your input/output filenames in the "Program arguments" box (separated by spaces).
Hopefully one of these fixes gets your program running smoothly!
内容的提问来源于stack exchange,提问作者Derek Xie

