{"record":{"id":"36f1b4ca37db31ff","repo":"lingochamp/FileDownloader","slug":"create-new-file-error-s","errorCode":null,"errorMessage":"create new file error  %s","messagePattern":"create new file error  (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/liulishuo/filedownloader/util/FileDownloadUtils.java","lineNumber":703,"sourceCode":"        }\n\n        //noinspection ConstantConditions\n        if (!FileDownloadUtils.isFilenameValid(path)) {\n            throw new RuntimeException(\n                    FileDownloadUtils.formatString(\"found invalid internal destination filename\"\n                            + \" %s\", path));\n        }\n\n        File file = new File(path);\n\n        if (file.exists() && file.isDirectory()) {\n            throw new RuntimeException(\n                    FileDownloadUtils.formatString(\"found invalid internal destination path[%s],\"\n                            + \" & path is directory[%B]\", path, file.isDirectory()));\n        }\n        if (!file.exists()) {\n            if (!file.createNewFile()) {\n                throw new IOException(\n                        FileDownloadUtils.formatString(\"create new file error  %s\",\n                                file.getAbsolutePath()));\n            }\n        }\n\n        return CustomComponentHolder.getImpl().createOutputStream(file);\n    }\n\n    public static boolean isBreakpointAvailable(final int id, final FileDownloadModel model) {\n        return isBreakpointAvailable(id, model, null);\n    }\n\n    /**\n     * @return can resume by break point\n     */\n    public static boolean isBreakpointAvailable(final int id, final FileDownloadModel model,\n                                                final Boolean outputStreamSupportSeek) {\n        if (model == null) {","sourceCodeStart":685,"sourceCodeEnd":721,"githubUrl":"https://github.com/lingochamp/FileDownloader/blob/6237a8cac174bcc916e4342b14ab1ab72a5768d4/library/src/main/java/com/liulishuo/filedownloader/util/FileDownloadUtils.java#L685-L721","documentation":"In createOutputStream, when the destination file does not exist the library calls File.createNewFile(); if that returns false the file could not be created and it throws this IOException naming the absolute path. This typically means the filesystem refused to create the file despite the path not existing.","triggerScenarios":"createOutputStream on a non-existent path where File.createNewFile() returns false — usually because the parent directory doesn't exist, the storage volume is read-only or unmounted, or a file with that name appeared concurrently between the exists() check and createNewFile().","commonSituations":"Downloading to external storage without WRITE_EXTERNAL_STORAGE permission, targeting removable SD card that was unmounted, parent folder deleted between calls, or concurrent downloads racing on the same path (file created by another thread causing createNewFile to return false).","solutions":["Verify storage permission is granted and requested at runtime (Android 6.0+ WRITE_EXTERNAL_STORAGE / scoped storage on 10+).","Ensure the parent directory exists before downloading: new File(file.getParent()).mkdirs().","Check storage state with Environment.getExternalStorageState() and fall back to internal storage if media is not MOUNTED.","Serialize or deduplicate downloads to the same path so two threads don't race on createNewFile()."],"exampleFix":"// before\nrequest.setPath(\"/sdcard/Download/myfile.apk\");\n// after\nFile dir = new File(\"/sdcard/Download\");\nif (!dir.exists()) dir.mkdirs();\nrequest.setPath(new File(dir, \"myfile.apk\").getAbsolutePath());","handlingStrategy":"try-catch","validationCode":"File dest = new File(path);\nif (!dest.getParentFile().exists()) dest.getParentFile().mkdirs();\nif (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {\n    // use internal storage instead\n}","typeGuard":null,"tryCatchPattern":"try {\n    downloader.create(path);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"create new file error\")) {\n        // check permissions / storage state, fall back to internal storage\n    }\n}","preventionTips":["Request and check storage permissions at runtime before downloading.","Create parent directories with mkdirs() before starting a download.","Check Environment.getExternalStorageState() before using external storage.","Avoid multiple threads creating the same destination file concurrently."],"tags":["file","io","storage","filedownloader"],"backgroundTag":"file-write-failed","analyzedSha":"6237a8cac174bcc916e4342b14ab1ab72a5768d4","analyzedAt":"2026-09-08T23:50:48.168Z","contentChangedAt":"2026-09-08T23:50:48.168Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}