MuntashirAkon/AppManager · error · IOException

Failed to create new file " + mNewName

Error message

Failed to create new file " + mNewName

What it means

AtomicExtendedFile.startWrite() wraps a FileNotFoundException thrown by newOutputStream() when the new file cannot be created/opened for writing. The library throws this IOException because the underlying filesystem refused to create the target file (parent directory exists and mkdirs was not needed, or creation failed after directory setup). It signals the atomic write could not even begin.

Source

Thrown at libcore/io/src/main/java/io/github/muntashirakon/io/AtomicExtendedFile.java:100

     */
    @WorkerThread
    @NonNull
    public FileOutputStream startWrite() throws IOException {
        if (mLegacyBackupName.exists()) {
            rename(mLegacyBackupName, mBaseName);
        }

        try {
            return mNewName.newOutputStream();
        } catch (FileNotFoundException e) {
            File parent = mNewName.getParentFile();
            if (!parent.mkdirs()) {
                throw new IOException("Failed to create directory for " + mNewName, e);
            }
            try {
                return mNewName.newOutputStream();
            } catch (FileNotFoundException e2) {
                throw new IOException("Failed to create new file " + mNewName, e2);
            }
        }
    }

    /**
     * Call when you have successfully finished writing to the stream
     * returned by {@link #startWrite()}.  This will close, sync, and
     * commit the new data.  The next attempt to read the atomic file
     * will return the new file stream.
     */
    public void finishWrite(@Nullable FileOutputStream str) {
        if (str == null) {
            return;
        }
        if (!sync(str)) {
            Log.e(TAG, "Failed to sync file output stream");
        }
        try {

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Verify the parent directory exists and is writable before calling startWrite(); recreate it if it was deleted.
  2. Check SELinux contexts and Unix permissions (ls -ld) on the target directory; fix with appropriate permissions or use a writable location.
  3. Confirm the target file name mNewName is a valid, non-empty path without illegal characters.
  4. Catch the IOException and retry or fall back to a different storage location.

Example fix

// before
OutputStream os = atomicFile.startWrite();
// after
File dir = atomicFile.getBaseFile().getParentFile();
if (dir != null && !dir.exists() && !dir.mkdirs()) {
    throw new IOException("Cannot create dir " + dir);
}
OutputStream os = atomicFile.startWrite();
Defensive patterns

Strategy: try-catch

Validate before calling

File dir = baseFile.getParentFile();
if (dir == null || !(dir.isDirectory() && dir.canWrite())) throw new IOException("Parent dir not writable: " + dir);

Try / catch

try (OutputStream os = atomicFile.startWrite()) {
    // write
} catch (IOException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Failed to create new file")) {
        // recreate parent dir or fall back to alternate location
    }
}

Prevention

When it happens

Trigger: Calling startWrite() (directly or via doWriteState) when mNewName.newOutputStream() throws FileNotFoundException: the target name is invalid, the parent directory is missing/renamed between check and open, or the process lacks write permission on the parent directory.

Common situations: Writing to a directory deleted concurrently by another process; path on storage that was unmounted; SELinux or filesystem permission denies creating files in the target directory; filename containing characters rejected by the filesystem.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/faa59c9ad207295a. Report an issue: GitHub.