简体   繁体   中英

types of byte data used in read method in fileinputstream

I have read about FileInputStream and I found that it have read method where it will read the byte data of the file.

What I want to know is what types of byte data does it read? Meaning does it use ASCII or Unicode or any other types?

From the documentation

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

ASCII, Unicode, ... apply in case of characters, not bytes. They are just bytes (8 bits).

You can use public InputStreamReader(InputStream in, Charset cs) where you specify the character set.

FileInputStream is a binary I/O class. Its the Text I/O that requires conversion from Unicode to a file specific encoding or vice versa but binary I/O does not require any conversion. When you read a byte from a file, the original byte is returned.

what i want to know is what types of byte data does it read? meaning does it use ASCII or Unicode or any other types?

As far as FileInputStream and its methods are concerned, there is only one "type" of data. And that type is "a sequence of bytes (or octets)".

Any other "types" are for another library (or application code) layer to deal with.

The "types" you refer to as ASCII and Unicode are character encodings (roughly speaking). (In fact, Unicode is NOT an encoding at all ... and doesn't exactly make sense in this context. Encodings for Unicode have names like "UTF-8" and "UTF-16" and so on - see http://en.wikipedia.org/wiki/Comparison_of_Unicode_encodings .)

Anyway, the standard way to "deal" with text file types (ie sequences of characters in some standard character encoding scheme) is to use a Reader class. And there is a wrapper class called InputStreamReader that is specifically designed for reading character data from an InputStream .

Other non-textual "types" of data are handled by classes like ZipInputStream , InflaterInputStream , GZIPInputStream , the image reader classes, and so on.

It doesn't change the file encoding. If you want to decode in the byte[] in a desired encoding, you may use charset to convert in desired charset as below:

   Charset cs = Charset.forName("UTF-8"); // breaks too
   String convertedString= new String(byteArray, cs);       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM