How can I read a file as unsigned bytes in Java?

前端 未结 5 1221
名媛妹妹
名媛妹妹 2021-01-12 14:39

How can I read a file to bytes in Java?

It is important to note that all the bytes need to be positive, i.e. the negative range cannot be used.

Can this be d

5条回答
  •  孤街浪徒
    2021-01-12 15:24

    Some testing revealed that this returns the unsigned byte values in [0…255] range one by one from the file:

    Reader bytestream = new BufferedReader(new InputStreamReader(
            new FileInputStream(inputFileName), "ISO-8859-1"));
    int unsignedByte;
    while((unsignedByte = bytestream.read()) != -1){
        // do work
    }
    

    It seems to be work for all bytes in the range, including those that no characters are defined for in ISO 8859-1.

提交回复
热议问题