Java:How can i get the encoding from inputStream?

后端 未结 2 1945
执笔经年
执笔经年 2020-12-31 10:41

I want get the encoding from a stream.

1st method - to use the InputStreamReader.

But it always return OS encode.

I         


        
2条回答
  •  别那么骄傲
    2020-12-31 11:05

    Let's resume the situation:

    • InputStream delivers bytes
    • *Readers deliver chars in some encoding
    • new InputStreamReader(inputStream) uses the operating system encoding
    • new InputStreamReader(inputStream, "UTF-8") uses the given encoding (here UTF-8)

    So one needs to know the encoding before reading. You did everything right using first a charset detecting class.

    Reading http://code.google.com/p/juniversalchardet/ it should handle UTF-8 and UTF-16. You might use the editor JEdit to verify the encoding, and see whether there is some problem.

提交回复
热议问题