Java IO: Byte & Char Arrays
Last updated
Was this helpful?
Last updated
Was this helpful?
Byte and char arrays are often used in Java to temporarily store data internally in an application. As such arrays are also a common source or destination of data. You may also prefer to load a file into an array, if you need to access the contents of that file a lot while the program is running. Of course you can access these arrays directly by indexing into them. But what if you have a component that is designed to read some specific data from an InputStream
or Reader
and not an array?
To make such a component read from the data from an array, you will have to wrap the byte or char array in an or . This way the bytes or chars available in the array can be read through the wrapping stream or reader.
Here is a simple example:
To do the same with a char array is pretty analogous to this example. Just wrap the char array in a CharArrayReader
and you are good to go.
Here is a simple example:
To do the same with a char array is pretty analogous to this example. Just wrap the char array in a CharArrayWriter
and you are good to go.
It is also possible to write data to an or . All you have to do is to create either a ByteArrayOutputStream
or CharArrayWriter
, and write your data to it, as you would to any other stream or writer. Once all the data is written to it, simply call the method toByteArray()
or toCharArray
, and all the data written is returned in array form.