Convert File with known encoding to UTF-8

后端 未结 1 570
情书的邮戳
情书的邮戳 2020-12-30 17:51

I need to convert text file to the String, which, finally, I should put as an input parameter (type InputStream) to IFile.create (Eclipse). Looking for the example or how to

相关标签:
1条回答
  • 2020-12-30 18:38

    You need to specify the encoding of the InputStreamReader using the Charset parameter.

                                        // ↓ whatever the input's encoding is
    Charset inputCharset = Charset.forName("ISO-8859-1");
    InputStreamReader isr = new InputStreamReader(fis, inputCharset));
    

    This also works:

    InputStreamReader isr = new InputStreamReader(fis, "ISO-8859-1"));
    

    See also:

    • InputStreamReader(InputStream in, Charset cs)
    • Charset.forName(String charsetName)
    • Java: How to determine the correct charset encoding of a stream
    • How to reliably guess the encoding between MacRoman, CP1252, Latin1, UTF-8, and ASCII
    • GuessEncoding - only works for UTF-8, UTF-16LE, UTF-16BE, and UTF-32 ☹
    • ICU Charset Detector
    • cpdetector, free java codepage detection
    • JCharDet (Java port of Mozilla charset detector) ironically, that page does not render the apostrophe in "Mozilla's" correctly

    SO search where I found all these links: https://stackoverflow.com/search?q=java+detect+encoding


    You can get the default charset - which is comes from the system the JVM is running on - at runtime via Charset.defaultCharset().

    0 讨论(0)
提交回复
热议问题