InputStreamReader buffering issue

后端 未结 6 891
感情败类
感情败类 2021-02-05 21:18

I am reading data from a file that has, unfortunately, two types of character encoding.

There is a header and a body. The header is always in ASCII and defines the char

6条回答
  •  借酒劲吻你
    2021-02-05 21:48

    It's even easier:

    As you said, your header is always in ASCII. So read the header directly from the InputStream, and when you're done with it, create the Reader with the correct encoding and read from it

    private Reader reader;
    private InputStream stream;
    
    public void read() {
        int c = 0;
        while ((c = stream.read()) != -1) {
            // Read encoding
            if ( headerFullyRead ) {
                reader = new InputStreamReader( stream, encoding );
                break;
            }
        }
        while ((c = reader.read()) != -1) {
            // Handle rest of file
        }
    }
    

提交回复
热议问题