asLody/VirtualApp · error · IOException

Couldn't create

Error message

Couldn't create 

What it means

After the initial open fails and the parent directory is successfully created, startWrite retries new FileOutputStream(mBaseName). If that second open also throws FileNotFoundException it throws this IOException 'Couldn't create <path>'. So the directory exists (or was just created) but the file itself cannot be created.

Solutions

  1. Check the path is not an existing directory and delete stale conflicting entries
  2. Verify free disk space and file ownership/permissions of the target path
  3. Catch IOException from startWrite and fall back to a writable cache dir location

Example fix

// before
File base = new File("/data/system/users/0/accounts.xml"); // maybe a dir or read-only
// after
File base = new File(context.getFilesDir(), "accounts.xml");
if (base.isDirectory()) base.delete();
AtomicFile af = new AtomicFile(base);
Defensive patterns

Strategy: try-catch

Validate before calling

if (base.isDirectory() || (base.exists() && !base.canWrite())) {
    throw new IOException("bad target: " + base);
}

Try / catch

try {
    FileOutputStream out = atomicFile.startWrite();
} catch (IOException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Couldn't create ")) {
        // free disk space / fix permissions / fall back to alternate path
    }
}

Prevention

When it happens

Trigger: Second FileOutputStream(mBaseName) attempt inside startWrite failing: permission denied on the file, a directory already exists at that path name, quota/disk-full, or SELinux policy blocking creation.

Common situations: Read-only filesystem; file owned by another uid after restore; disk full on /data; the base name collides with an existing directory.

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 asLody/VirtualApp@666fefcb5d (2026-09-09). Data as JSON: /api/errors/eca08f300159b1a9. Report an issue: GitHub.

Appendix: source

Thrown at VirtualApp/lib/src/main/java/com/lody/virtual/helper/utils/AtomicFile.java:93

                    Log.w("AtomicFile", "Couldn't rename file " + mBaseName
                            + " to backup file " + mBackupName);
                }
            } else {
                mBaseName.delete();
            }
        }
        FileOutputStream str = null;
        try {
            str = new FileOutputStream(mBaseName);
        } catch (FileNotFoundException e) {
            File parent = mBaseName.getParentFile();
            if (!parent.mkdir()) {
                throw new IOException("Couldn't create directory " + mBaseName);
            }
            try {
                str = new FileOutputStream(mBaseName);
            } catch (FileNotFoundException e2) {
                throw new IOException("Couldn't create " + mBaseName);
            }
        }
        return str;
    }

    /**
     * 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(FileOutputStream str) {
        if (str != null) {
            sync(str);
            try {
                str.close();
                mBackupName.delete();
            } catch (IOException e) {

View on GitHub (pinned to 666fefcb5d)