Junior — Middle
Can you give examples of methods related to different stream types?
sobes.tech AI
Answer from AI
In Java, streams come in different types, such as InputStream and OutputStream for working with byte streams, and Reader and Writer for character streams.
Examples of methods:
read()— reads data from InputStream or Reader.write(int b)— writes a byte to OutputStream.close()— closes the stream and releases resources.flush()— flushes the buffer, writing data to the target resource.
Example of using InputStream:
InputStream input = new FileInputStream("file.txt");
int data = input.read();
while(data != -1) {
System.out.print((char) data);
data = input.read();
}
input.close();
These methods allow efficient reading and writing of data in various formats.