chinabugotech/hutool · error · IORuntimeException
File not exist: {}
Error message
File not exist: {} What it means
FileReader.checkFile() is invoked from the FileReader(File, Charset) constructor, so it runs at construction time rather than at read time. It throws 'File not exist' when file.exists() is false. Because FileUtil.file(...) resolves relative paths against the classpath, a relative path that does not match a classpath resource also surfaces here. The File object's toString is appended to the message.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/io/file/FileReader.java:300
* Reader处理接口
*
* @author Luxiaolei
*
* @param <T> Reader处理返回结果类型
*/
public interface ReaderHandler<T> {
T handle(BufferedReader reader) throws IOException;
}
// -------------------------------------------------------------------------- Interface end
/**
* 检查文件
*
* @throws IORuntimeException IO异常
*/
private void checkFile() throws IORuntimeException {
if (false == file.exists()) {
throw new IORuntimeException("File not exist: " + file);
}
if (false == file.isFile()) {
throw new IORuntimeException("Not a file:" + file);
}
}
}
View on GitHub (pinned to 8870454b2a)
Solutions
- Verify the path and that the file exists (Files.exists(Path)) before constructing the reader.
- For relative paths, confirm whether you need a classpath resource or a filesystem path, and use an absolute path if filesystem-relative.
- Check the working directory / classpath and create or restore the missing file.
- If the file may legitimately be absent, guard construction with an existence check and provide a fallback.
Example fix
// before: relative path resolved against classpath, file absent -> 'File not exist'
FileReader reader = FileReader.create("config/app.properties");
// after: resolve an explicit filesystem path and verify it exists
Path p = Paths.get("/etc/app/config/app.properties");
if (!Files.exists(p)) {
throw new IllegalStateException("Missing config: " + p);
}
FileReader reader = FileReader.create(p.toFile()); Defensive patterns
Strategy: validation
Validate before calling
Path p = Paths.get(pathArg);
if (!Files.exists(p)) {
throw new IllegalArgumentException("File not found: " + p);
}
FileReader reader = FileReader.create(p.toFile()); Type guard
boolean isReadableFile(String path) {
File f = new File(path);
return f.exists() && f.isFile();
} Try / catch
try {
FileReader reader = FileReader.create(file);
} catch (IORuntimeException e) {
if (e.getMessage() != null && e.getMessage().startsWith("File not exist")) {
// missing file: create a default, restore from backup, or surface a clean error
throw new ConfigurationException("Required file missing: " + file, e);
}
throw e;
} Prevention
- Resolve and validate file paths against the filesystem before constructing a reader.
- Remember relative paths are classpath-relative via FileUtil.file; use absolute paths for filesystem files.
- Check Files.exists in tests and startup validation so missing files fail fast with a clear message.
- Guard deployments that the required config/resource files are present.
- Distinguish 'expected absent' from 'must exist' cases explicitly in code.
When it happens
Trigger: Constructing a FileReader via new FileReader(file), new FileReader(filePath), or FileReader.create(file[, charset]) when the resolved File does not exist on disk (and, for relative paths, is not resolvable on the classpath). The error is thrown from the constructor, before any read call.
Common situations: Typo or wrong path; file deleted between resolving the path and constructing the reader; a relative path intended as filesystem-relative but interpreted as classpath-relative by FileUtil.file; deployment missing a config file; working directory differs from the expected one; environment-specific path differences (dev vs prod).
Related errors
- Not a file: {}
- Src [{}] is a directory but dest [{}] is a file!
- File is larger then max array size
- File length is [{}] but read [{}]!
- TTS请求失败:
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/27cf05cb153242fd.
Report an issue: GitHub.