{"record":{"id":"0d83c02347b4beb7","repo":"alibaba/spring-cloud-alibaba","slug":"file-cannot-be-read","errorCode":null,"errorMessage":"File '{}' cannot be read","messagePattern":"File '(.+?)' cannot be read","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/io/FileUtils.java","lineNumber":60,"sourceCode":"\t * exception will have been thrown.\n\t * <p>\n\t * An exception is thrown if the file does not exist. An exception is thrown if the\n\t * file object exists but is a directory. An exception is thrown if the file exists\n\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 {","sourceCodeStart":42,"sourceCodeEnd":78,"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#L42-L78","documentation":"Thrown by FileUtils.openInputStream when the file exists and is not a directory, but the current process lacks read permission (file.canRead() returns false). This provides a clearer error message than the generic exception from new FileInputStream(file), which may throw a less descriptive AccessDeniedException on some platforms.","triggerScenarios":"Calling FileUtils.openInputStream(file) where the file exists, is a regular file, but the OS file permissions deny read access to the JVM process. Common in containerized deployments where file ownership or mode bits differ from the running user.","commonSituations":"1) Docker/Kubernetes container running as non-root user while files are owned by root with mode 600. 2) File extracted from an archive with restrictive permissions. 3) SELinux or AppArmor denying read access. 4) NFS mount with mapping that strips read permission.","solutions":["Grant read permission to the JVM process user: chmod +r <file> or chmod 644 <file>.","If running in a container, ensure the Dockerfile or init script sets correct file ownership and permissions (chown / chmod)."],"exampleFix":"# before (file owned by root, app runs as appuser)\n# ls -l config.yml -> -rw------- root root\n\n# after (fix permissions)\nchmod 644 config.yml\n# or change ownership\nchown appuser:appuser config.yml","handlingStrategy":"validation","validationCode":"File file = new File(path);\nif (!file.canRead()) {\n    throw new IllegalStateException(\n        \"Cannot read file: \" + file.getAbsolutePath()\n        + \" — check permissions for user: \" + System.getProperty(\"user.name\"));\n}\nFileInputStream fis = FileUtils.openInputStream(file);","typeGuard":"import java.io.File;\n\nboolean isReadableFile(File file) {\n    return file != null && file.exists() && file.isFile() && file.canRead();\n}","tryCatchPattern":"try {\n    InputStream in = FileUtils.openInputStream(file);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"cannot be read\")) {\n        log.error(\"Permission denied reading {} — current user: {}\",\n            file, System.getProperty(\"user.name\"));\n    }\n    throw e;\n}","preventionTips":["In Dockerfiles, set file permissions explicitly: RUN chmod 644 <file> or COPY --chmod=644.","Run containers as a non-root user and ensure config files are owned by that user.","Add file-permission assertions to deployment scripts or Helm chart init containers."],"tags":["file-io","commons-io","permissions","filesystem","deployment"],"backgroundTag":null,"analyzedSha":"115d5901102009492e05d5ec18c3f79cad4077d0","analyzedAt":"2026-08-14T04:47:13.900Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}