java encoding ai_generated true

java.nio.charset.MalformedInputException: Input length = 1 — non-ASCII path or filename

ID: java/malformedinputexception-nonascii-path

Also available as: JSON · Markdown
85%Fix Rate
88%Confidence
4Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
17 active

Root Cause

Java Files.readAllLines() or similar API throws MalformedInputException when file path or content contains Korean/CJK and no charset is specified.

generic

Workarounds

  1. 95% success Explicitly specify StandardCharsets.UTF_8 in all file read/write operations
    Files.readAllLines(path, StandardCharsets.UTF_8)  // never rely on default charset

    Sources: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Files.html

  2. 88% success Set -Dfile.encoding=UTF-8 JVM argument and use InputStreamReader with explicit charset
    java -Dfile.encoding=UTF-8 -jar app.jar  // also use new InputStreamReader(is, StandardCharsets.UTF_8)

    Sources: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/InputStreamReader.html

Dead Ends

Common approaches that don't work:

  1. Wrap in try-catch and skip the file 90% fail

    Silently skipping files with Korean names hides real data; does not fix the encoding issue

  2. Convert filename to ASCII with Normalizer 85% fail

    Normalizer handles decomposition, not transliteration; Korean chars have no ASCII equivalent