{"record":{"id":"9475ba9077cf188c","repo":"MuntashirAkon/AppManager","slug":"directory-cannot-be-converted-to-file","errorCode":null,"errorMessage":"Directory cannot be converted to file","messagePattern":"Directory cannot be converted to file","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/io/PathImpl.java","lineNumber":499,"sourceCode":"            throw new IOException(\"Empty display name.\");\n        }\n        if (displayName.indexOf(File.separatorChar) != -1) {\n            throw new IllegalArgumentException(\"Display name contains file separator.\");\n        }\n        DocumentFile documentFile = getRealDocumentFile(this.documentFile);\n        if (!documentFile.isDirectory()) {\n            throw new IOException(\"Current file is not a directory.\");\n        }\n        String extension = null;\n        if (mimeType != null) {\n            extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);\n        } else mimeType = DEFAULT_MIME;\n        String nameWithExtension = displayName + (extension != null ? \".\" + extension : \"\");\n        checkVfs(Paths.appendPathSegment(documentFile.getUri(), nameWithExtension));\n        DocumentFile file = documentFile.findFile(displayName);\n        if (file != null) {\n            if (file.isDirectory()) {\n                throw new IOException(\"Directory cannot be converted to file\");\n            }\n            return new PathImpl(context, file);\n        }\n        file = documentFile.createFile(mimeType, displayName);\n        if (file == null) {\n            throw new IOException(\"Could not create \" + documentFile.getUri() + File.separatorChar + nameWithExtension + \" with type \" + mimeType);\n        }\n        return new PathImpl(context, file);\n    }\n\n    @NonNull\n    public Path findOrCreateDirectory(@NonNull String displayName) throws IOException {\n        displayName = Paths.sanitize(displayName, true);\n        if (displayName == null) {\n            throw new IOException(\"Empty display name.\");\n        }\n        if (displayName.indexOf(File.separatorChar) != -1) {\n            throw new IllegalArgumentException(\"Display name contains file separator.\");","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/io/PathImpl.java#L481-L517","documentation":"During findOrCreateFile(), if an existing child matches the display name but is a directory, the method throws IOException(\"Directory cannot be converted to file\"). The library will not delete or overwrite a directory to satisfy a file creation. The name is already occupied by a directory.","triggerScenarios":"Calling findOrCreateFile(\"name\", mime) when a directory named \"name\" already exists under the target directory (found via documentFile.findFile).","commonSituations":"App version change where a formerly-directory location is now used as a file (e.g. \"cache\" as dir then as file); user-created folder with the same name as a planned file; restoring backups that map dirs onto file names.","solutions":["Check hasFile(name) and whether the existing entry isDirectory() first; choose a different filename or migrate the data.","Delete the directory (after confirming it is safe/empty) before creating the file.","Rename the existing directory, then create the file.","Version your names (e.g. \"cache-v2\") to avoid collisions across app upgrades."],"exampleFix":"// before\npath.findOrCreateFile(\"cache\", mime); // IOException: Directory cannot be converted to file\n// after\nif (path.hasFile(\"cache\") && path.findFile(\"cache\").isDirectory()) {\n    path.findFile(\"cache\").delete(); // after verifying it is safe\n}\npath.findOrCreateFile(\"cache\", mime);","handlingStrategy":"validation","validationCode":"if (path.hasFile(name)) {\n    Path existing = path.findFile(name);\n    if (existing.isDirectory()) { /* rename, delete, or pick a new name */ }\n}","typeGuard":"boolean isFileCandidate(Path p, String n) { try { return !p.hasFile(n) || !p.findFile(n).isDirectory(); } catch (FileNotFoundException e) { return true; } }","tryCatchPattern":"try { path.findOrCreateFile(name, mime); } catch (IOException e) { if (\"Directory cannot be converted to file\".equals(e.getMessage())) { /* choose alternate name or migrate */ } }","preventionTips":["Check entry type before reusing a name across app versions","Version names that changed kind (dir<->file)","Never silently delete directories to make room for files"],"tags":["android","filesystem","conflict","type-mismatch"],"backgroundTag":"incompatible-source-type","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}