{"record":{"id":"27cf05cb153242fd","repo":"chinabugotech/hutool","slug":"file-not-exist-27cf05","errorCode":null,"errorMessage":"File not exist: {}","messagePattern":"File not exist: (.+?)","errorType":"exception","errorClass":"IORuntimeException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/io/file/FileReader.java","lineNumber":300,"sourceCode":"\t * Reader处理接口\n\t *\n\t * @author Luxiaolei\n\t *\n\t * @param <T> Reader处理返回结果类型\n\t */\n\tpublic interface ReaderHandler<T> {\n\t\tT handle(BufferedReader reader) throws IOException;\n\t}\n\t// -------------------------------------------------------------------------- Interface end\n\n\t/**\n\t * 检查文件\n\t *\n\t * @throws IORuntimeException IO异常\n\t */\n\tprivate void checkFile() throws IORuntimeException {\n\t\tif (false == file.exists()) {\n\t\t\tthrow new IORuntimeException(\"File not exist: \" + file);\n\t\t}\n\t\tif (false == file.isFile()) {\n\t\t\tthrow new IORuntimeException(\"Not a file:\" + file);\n\t\t}\n\t}\n}\n","sourceCodeStart":282,"sourceCodeEnd":307,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/io/file/FileReader.java#L282-L307","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before: relative path resolved against classpath, file absent -> 'File not exist'\nFileReader reader = FileReader.create(\"config/app.properties\");\n\n// after: resolve an explicit filesystem path and verify it exists\nPath p = Paths.get(\"/etc/app/config/app.properties\");\nif (!Files.exists(p)) {\n    throw new IllegalStateException(\"Missing config: \" + p);\n}\nFileReader reader = FileReader.create(p.toFile());","handlingStrategy":"validation","validationCode":"Path p = Paths.get(pathArg);\nif (!Files.exists(p)) {\n    throw new IllegalArgumentException(\"File not found: \" + p);\n}\nFileReader reader = FileReader.create(p.toFile());","typeGuard":"boolean isReadableFile(String path) {\n    File f = new File(path);\n    return f.exists() && f.isFile();\n}","tryCatchPattern":"try {\n    FileReader reader = FileReader.create(file);\n} catch (IORuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"File not exist\")) {\n        // missing file: create a default, restore from backup, or surface a clean error\n        throw new ConfigurationException(\"Required file missing: \" + file, e);\n    }\n    throw e;\n}","preventionTips":["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."],"tags":["io","filesystem","path","classpath","hutool"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}