{"record":{"id":"f4e3c0c407f1779e","repo":"alibaba/spring-cloud-alibaba","slug":"file-does-not-exist","errorCode":null,"errorMessage":"File '{}' does not exist","messagePattern":"File '(.+?)' does not exist","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/io/FileUtils.java","lineNumber":64,"sourceCode":"\t * but cannot be read.\n\t * @param file the file to open for input, must not be {@code null}\n\t * @return a new {@link java.io.FileInputStream} for the specified file\n\t * @throws java.io.FileNotFoundException if the file does not exist\n\t * @throws IOException if the file object is a directory\n\t * @throws IOException if the file cannot be read\n\t * @since 1.3\n\t */\n\tpublic static FileInputStream openInputStream(final File file) throws IOException {\n\t\tif (file.exists()) {\n\t\t\tif (file.isDirectory()) {\n\t\t\t\tthrow new IOException(\"File '\" + file + \"' exists but is a directory\");\n\t\t\t}\n\t\t\tif (!file.canRead()) {\n\t\t\t\tthrow new IOException(\"File '\" + file + \"' cannot be read\");\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tthrow new FileNotFoundException(\"File '\" + file + \"' does not exist\");\n\t\t}\n\t\treturn new FileInputStream(file);\n\t}\n\n\t// -----------------------------------------------------------------------\n\t/**\n\t * Reads the contents of a file into a String. The file is always closed.\n\t * @param file the file to read, must not be {@code null}\n\t * @param encoding the encoding to use, {@code null} means platform default\n\t * @return the file contents, never {@code null}\n\t * @throws IOException in case of an I/O error\n\t */\n\tpublic static String readFileToString(final File file, final Charset encoding)\n\t\t\tthrows IOException {\n\t\ttry (InputStream in = openInputStream(file)) {\n\t\t\treturn IOUtils.toString(in, Charsets.toCharset(encoding));\n\t\t}\n\t}","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/alibaba/spring-cloud-alibaba/blob/115d5901102009492e05d5ec18c3f79cad4077d0/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/io/FileUtils.java#L46-L82","documentation":"Thrown by FileUtils.openInputStream as a FileNotFoundException when the file does not exist on the filesystem at all. This is the first check in the method — if file.exists() is false, it immediately throws rather than attempting to construct a FileInputStream (which would produce a less informative error).","triggerScenarios":"Calling FileUtils.openInputStream(file) (or readFileToString) where the path does not resolve to any existing filesystem entry. Common when a file path is configured but the file was never created, was deleted, or is on a mount point that is not attached.","commonSituations":"1) Configured file path points to a location that does not exist (typo, wrong directory). 2) A volume mount in Kubernetes/Docker is not attached, so the path is empty. 3) A file generated by a previous build step is missing in the deployed artifact. 4) Relative path resolved against an unexpected working directory.","solutions":["Verify the file exists at the specified absolute path: ls -l <path>.","If using a relative path, check the JVM working directory or use an absolute path / Spring ResourceUtils.getResource() for classpath resources.","If the file is supposed to be generated, ensure the generation step ran before this code executes.","If running in a container, verify the volume mount and that the file is present in the image or mounted volume."],"exampleFix":"// before\nFileUtils.openInputStream(new File(\"config/rules.json\")); // wrong relative path\n\n// after\nFileUtils.openInputStream(new File(\"/opt/app/config/rules.json\")); // absolute path\n// or for classpath resources:\nFileUtils.openInputStream(ResourceUtils.getFile(\"classpath:rules.json\"));","handlingStrategy":"validation","validationCode":"File file = new File(path);\nif (!file.exists()) {\n    throw new FileNotFoundException(\n        \"File not found: \" + file.getAbsolutePath()\n        + \" (working dir: \" + System.getProperty(\"user.dir\") + \")\");\n}\nFileInputStream fis = FileUtils.openInputStream(file);","typeGuard":"import java.io.File;\n\nboolean fileExists(File file) {\n    return file != null && file.exists() && file.isFile();\n}","tryCatchPattern":"try {\n    InputStream in = FileUtils.openInputStream(file);\n} catch (FileNotFoundException e) {\n    log.error(\"File not found: {} — check path and mount points\", file.getAbsolutePath());\n    throw e;\n}","preventionTips":["Use absolute paths in configuration to avoid working-directory ambiguity.","For classpath resources that must be filesystem-resolvable, extract them at startup using a resource loader.","Add volume-mount validation in Kubernetes init containers.","Log the resolved absolute path during startup to catch path issues early."],"tags":["file-io","commons-io","not-found","filesystem"],"backgroundTag":null,"analyzedSha":"115d5901102009492e05d5ec18c3f79cad4077d0","analyzedAt":"2026-08-14T04:47:13.900Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}