{"record":{"id":"e9c739c35e8bdc32","repo":"alibaba/arthas","slug":"directory-could-not-be-created","errorCode":null,"errorMessage":"Directory '{}' could not be created","messagePattern":"Directory '(.+?)' could not be created","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/taobao/arthas/core/util/FileUtils.java","lineNumber":86,"sourceCode":"     * @return a new {@link FileOutputStream} for the specified file\n     * @throws IOException if the file object is a directory\n     * @throws IOException if the file cannot be written to\n     * @throws IOException if a parent directory needs creating but that fails\n     * @since 2.1\n     */\n    public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {\n        if (file.exists()) {\n            if (file.isDirectory()) {\n                throw new IOException(\"File '\" + file + \"' exists but is a directory\");\n            }\n            if (!file.canWrite()) {\n                throw new IOException(\"File '\" + file + \"' cannot be written to\");\n            }\n        } else {\n            File parent = file.getParentFile();\n            if (parent != null) {\n                if (!parent.mkdirs() && !parent.isDirectory()) {\n                    throw new IOException(\"Directory '\" + parent + \"' could not be created\");\n                }\n            }\n        }\n        return new FileOutputStream(file, append);\n    }\n\n    private static boolean isAuthCommand(String command) {\n        // 需要改写 auth command, TODO 更准确应该是用mask去掉密码信息\n        return command != null && command.trim().startsWith(ArthasConstants.AUTH);\n    }\n\n    /**\n     * save the command history to the given file, data will be overridden.\n     * @param history the command history, each represented by an int array\n     * @param file the file to save the history\n     */\n    public static void saveCommandHistory(List<int[]> history, File file) {\n        try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) {","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/alibaba/arthas/blob/21cf2e9ba52b305290be7223b980ff504bb9cb5b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java#L68-L104","documentation":"FileUtils.openOutputStream() throws this IOException when the target file does not exist, a parent directory is specified, but parent.mkdirs() fails AND the parent is not already a directory. This covers the case where the parent path cannot be created — typically due to permissions or a conflicting non-directory entry.","triggerScenarios":"Calling openOutputStream(file, append) where the file does not exist, file.getParentFile() is non-null, and parent.mkdirs() returns false while parent.isDirectory() is also false.","commonSituations":"The parent path collides with an existing file (e.g. /tmp/output exists as a file but /tmp/output/result.log is requested); insufficient permissions to create directories; a component of the parent path is a read-only mount; typo in the configured directory path.","solutions":["Manually create the parent directory beforehand and verify it exists: mkdir -p <parent>.","Remove or rename any non-directory entry blocking the parent path.","Verify the JVM process has write+execute permissions on the ancestor directories.","Fix the configured path to avoid the conflicting component."],"exampleFix":"// before: /tmp/arthas is a file, not a directory\nFile out = new File(\"/tmp/arthas/result.log\");\nFileUtils.openOutputStream(out, false); // throws 'Directory could not be created'\n\n// after: remove conflicting entry, then write\n// rm /tmp/arthas  (it was a file)\nFile out = new File(\"/tmp/arthas/result.log\");\nout.getParentFile().mkdirs();\nFileUtils.openOutputStream(out, false);","handlingStrategy":"validation","validationCode":"File file = new File(outputPath);\nFile parent = file.getParentFile();\nif (parent != null && !parent.exists() && !parent.mkdirs()) {\n    throw new IllegalStateException(\"Cannot create parent dir: \" + parent);\n}\nFileUtils.openOutputStream(file, append);","typeGuard":null,"tryCatchPattern":"try {\n    FileUtils.openOutputStream(file, append);\n} catch (IOException e) {\n    if (e.getMessage().contains(\"could not be created\")) {\n        // resolve blocking entry or fix permissions, then retry\n        File parent = file.getParentFile();\n        if (parent.exists() && !parent.isDirectory()) parent.delete();\n        parent.mkdirs();\n        FileUtils.openOutputStream(file, append);\n    } else { throw e; }\n}","preventionTips":["Call parent.mkdirs() explicitly before openOutputStream and verify the result.","Ensure no file/directory name collisions on the parent path."],"tags":["arthas","file-io","filesystem","permissions"],"backgroundTag":null,"analyzedSha":"21cf2e9ba52b305290be7223b980ff504bb9cb5b","analyzedAt":"2026-08-14T00:57:07.243Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}