I/O Stream Abstraction
Cangjie's I/O system is built on the stream abstraction. InputStream and OutputStream are the two core interfaces for reading and writing byte data, representing input streams and output streams respectively. The input stream provides a read method to read data into a buffer, while the output stream provides a write method to write data out. This design unifies access to different data sources such as files, networks, and memory.
ByteBuffer is a memory stream that lets you read and write byte data in memory without accessing the file system. First call write to write data, then call read to read it back. Reading and writing share the same position pointer. The example on the right demonstrates writing string byte data to a ByteBuffer, then reading it back and converting it to a string for display.
String streams StringReader and StringWriter add text processing capabilities to byte streams. StringReader provides a readln() method to read text line by line, which is more convenient and efficient than manually converting byte data.
Byte). Strings need to be converted to byte arrays via toArray() for writing, and converted back to strings using String.fromUtf8() after reading.After running the code, try writing more lines to the ByteBuffer and reading them line by line with StringReader.