Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

FileIOUtils is a final utility class whose private constructor throws UnsupportedOperationException("u can't instantiate me..."). All file-I/O helpers (writeFileFromIS, writeFileFromBytes, readFile2Bytes, etc.) are static and share a static sBufferSize; no instance is needed and the constructor only blocks instantiation, per the library-wide *Utils convention.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/FileIOUtils.java:41

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2017/06/22
 *     desc  : utils about file io
 * </pre>
 */
public final class FileIOUtils {

    private static int sBufferSize = 524288;

    private FileIOUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    ///////////////////////////////////////////////////////////////////////////
    // writeFileFromIS without progress
    ///////////////////////////////////////////////////////////////////////////

    /**
     * Write file from input stream.
     *
     * @param filePath The path of file.
     * @param is       The input stream.
     * @return {@code true}: success<br>{@code false}: fail
     */
    public static boolean writeFileFromIS(final String filePath, final InputStream is) {
        return writeFileFromIS(UtilsBridge.getFileByPath(filePath), is, false, null);
    }

    /**

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use the static API directly: FileIOUtils.writeFileFromBytes(...), FileIOUtils.readFile2Bytes(...), etc.
  2. Exclude final utility classes from any reflective instantiation framework.
  3. In a contract test, assert UnsupportedOperationException is thrown by the reflected constructor.

Example fix

// before (reflection)
FileIOUtils f = FileIOUtils.class.getDeclaredConstructor().newInstance();

// after
FileIOUtils.writeFileFromBytes(path, bytes, true);
Defensive patterns

Strategy: validation

Validate before calling

// Never construct FileIOUtils; it is static-only.
if (clazz == FileIOUtils.class || Modifier.isFinal(clazz.getModifiers())) {
    // skip instantiation; use FileIOUtils.xxx() statically
}

Type guard

if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
    // static-only; do not newInstance()
}

Try / catch

try {
    Constructor<?> c = FileIOUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException e) {
    // expected; use FileIOUtils.xxx() instead
}

Prevention

When it happens

Trigger: Reflective construction via getDeclaredConstructor().newInstance() with setAccessible, or a framework that new-ups classes by Class reference. A direct `new FileIOUtils()` fails to compile because the constructor is private.

Common situations: Coverage tooling that instantiates all classes; DI containers scanning packages; a developer who assumes instance methods; reflective test harnesses that exercise all constructors.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/76b2aa180019e07a. Report an issue: GitHub.