{"record":{"id":"b7e476345b722460","repo":"alibaba/arthas","slug":"file-file-cannot-be-written-to","errorCode":null,"errorMessage":"File '{file}' cannot be written to","messagePattern":"File '(.+?)' cannot be written to","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/com/taobao/arthas/common/FileUtils.java","lineNumber":119,"sourceCode":"\t * cannot be written to. An exception is thrown if the parent directory cannot\n\t * be created.\n\t *\n\t * @param file   the file to open for output, must not be {@code null}\n\t * @param append if {@code true}, then bytes will be added to the end of the\n\t *               file rather than overwriting\n\t * @return a new {@link FileOutputStream} for the specified file\n\t * @throws IOException if the file object is a directory\n\t * @throws IOException if the file cannot be written to\n\t * @throws IOException if a parent directory needs creating but that fails\n\t * @since 2.1\n\t */\n\tpublic static FileOutputStream openOutputStream(final File file, final boolean append) 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.canWrite()) {\n\t\t\t\tthrow new IOException(\"File '\" + file + \"' cannot be written to\");\n\t\t\t}\n\t\t} else {\n\t\t\tfinal File parent = file.getParentFile();\n\t\t\tif (parent != null) {\n\t\t\t\tif (!parent.mkdirs() && !parent.isDirectory()) {\n\t\t\t\t\tthrow new IOException(\"Directory '\" + parent + \"' could not be created\");\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn new FileOutputStream(file, append);\n\t}\n\t\n    /**\n     * Reads the contents of a file into a byte array.\n     * The file is always closed.\n     *\n     * @param file the file to read, must not be {@code null}\n     * @return the file contents, never {@code null}","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/alibaba/arthas/blob/21cf2e9ba52b305290be7223b980ff504bb9cb5b/common/src/main/java/com/taobao/arthas/common/FileUtils.java#L101-L137","documentation":"FileUtils.openOutputStream(file, append): when the target exists, is not a directory, but file.canWrite() is false, it throws IOException(\"File '<file>' cannot be written to\"). The existing file is present but not writable by the current process.","triggerScenarios":"Opening an existing file for output when the process lacks write permission on it (read-only file, owned by another user, immutable attribute set).","commonSituations":"File created by a different user/service and not chowned; container uid mismatch vs host file ownership; file marked read-only or immutable (chattr +i); SELinux/AppArmor denying writes.","solutions":["Fix permissions: chmod u+w <file> or chown to the running user.","Run the Arthas/agent process under a user that owns the file.","Remove immutable attribute if set (chattr -i).","Point output at a writable location instead."],"exampleFix":"# before\n# file owned by root, process runs as app user\nFileUtils.openOutputStream(new File(\"/var/log/arthas.log\"), true);  // throws\n\n# after\nchown app:app /var/log/arthas.log && chmod u+w /var/log/arthas.log","handlingStrategy":"validation","validationCode":"if (file.exists() && file.isFile() && !file.canWrite()) {\n    throw new IOException(\"file not writable: \" + file);\n}","typeGuard":"static boolean isWritable(File f) {\n    return f != null && (!f.exists() || f.canWrite());\n}","tryCatchPattern":"try {\n    return FileUtils.openOutputStream(file, append);\n} catch (IOException e) {\n    if (e.getMessage().contains(\"cannot be written to\")) {\n        // fix ownership/permissions, then retry\n    } else throw e;\n}","preventionTips":["Run the process under a user that owns (or can write) the output file.","chmod u+w output files; remove immutable attributes.","In containers, match the uid to the host file owner."],"tags":["filesystem","io","permissions","common"],"backgroundTag":null,"analyzedSha":"21cf2e9ba52b305290be7223b980ff504bb9cb5b","analyzedAt":"2026-08-14T00:57:07.243Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}