Tasks studies - laboratory
A stream is a sequence of data, typically bytes. The origin and type of the data sequence depend on the environment. Java provides basic stream types for input and output operations. The InputStream
class handles input, while the OutputStream
class handles output. Streams are associated with devices such as:
Data types used by streams to transmit information include:
byte
,String
,Object
.AudioInputStream
ByteArrayInputStream
FileInputStream
FilterInputStream
BufferedInputStream
CheckedInputStream
CipherInputStream
DataInputStream
DigestInputStream
InflaterInputStream
LineNumberInputStream
ProgressMonitorInputStream
PushbackInputStream
InputStream
ObjectInputStream
PipedInputStream
SequenceInputStream
StringBufferInputStream
ByteArrayOutputStream
FileOutputStream
FilterOutputStream
BufferedOutputStream
CheckedOutputStream
CipherOutputStream
DataOutputStream
DeflaterOutputStream
DigestOutputStream
PrintStream
ObjectOutputStream
OutputStream
PipedOutputStream
Streams in Java are managed using the InputStream
and OutputStream
classes, which represent them as sequences of byte
elements. Data formatting in streams is often required, which is done using various formatting classes that extend InputStream
and OutputStream
.
For example, writing text to the console is done using System.out.println()
, where out
is an object of the PrintStream
class that formats byte sequences into text.
Java also includes the Reader
and Writer
classes for handling text data (String
).
InputStream
The InputStream
class is abstract and provides methods to read and control bytes from a stream. As an abstract class, its objects cannot be instantiated directly, but they can be obtained through methods like System.in
or getInputStream()
in the Socket
class.
Key methods in InputStream
:
int read(byte b[]);
int read(byte b[], int offset, int length);
long skip(long n);
int available();
boolean markSupported();
synchronized void mark(int readlimit);
synchronized void reset();
void close();
Most methods, except markSupported()
and mark()
, may throw exceptions (e.g., IOException
).
OutputStream
The OutputStream
class is also abstract, with a key method write()
for writing bytes to a stream.
Key methods in OutputStream
:
void write(byte[] b, int off, int len);
void write(byte[] b);
void close();
void flush();
Example: Simple Stream Usage
import java.io.*;
public class Echo {
public static void main(String args[]) {
byte b[] = new byte[100];
try {
System.in.read(b);
System.out.write(b);
System.out.flush();
} catch (IOException ioe) {
System.out.println("Input/output error");
}
}
}
The File
class represents abstract file and directory paths. Paths are either relative or absolute, depending on the system.
Examples:
/usr/local/bin
C:\Program Files
projects/java
projects\java
Example:
File file = new File("C:\\data.txt");
File dir = new File("C:\\Documents");
File newFile = new File(dir, "new_file.txt");
Character streams handle Unicode characters (16-bit codes). Classes FileReader
and FileWriter
are used for reading and writing text files, respectively.
FileReader file = new FileReader("input.txt");
int character;
while ((character = file.read()) != -1) {
System.out.print((char) character);
}
file.close();
FileWriter file = new FileWriter("output.txt");
file.write("Hello, World!");
file.close();
Buffered streams improve efficiency by reading or writing large chunks of data at once.
Example:
import java.io.*;
public class Copy {
public static void main(String[] args) {
try {
FileReader input = new FileReader("input.txt");
BufferedReader bufferedInput = new BufferedReader(input);
FileWriter output = new FileWriter("output.txt");
BufferedWriter bufferedOutput = new BufferedWriter(output);
String line;
while ((line = bufferedInput.readLine()) != null) {
bufferedOutput.write(line);
bufferedOutput.newLine();
}
bufferedInput.close();
bufferedOutput.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Binary streams handle raw binary data. Classes FileInputStream
and FileOutputStream
are used for reading and writing binary files.
FileInputStream file = new FileInputStream("input.dat");
int byteData;
while ((byteData = file.read()) != -1) {
System.out.println(byteData);
}
file.close();
FileOutputStream file = new FileOutputStream("output.dat");
file.write(255); // Writes a single byte
file.close();
Java provides classes for handling compressed files (e.g., ZIP, GZIP) in the java.util.zip
package.
Example: GZIP Compression
import java.io.*;
import java.util.zip.*;
public class Compression {
public static void main(String[] args) {
try {
FileInputStream input = new FileInputStream("input.txt");
FileOutputStream output = new FileOutputStream("output.gz");
GZIPOutputStream gzip = new GZIPOutputStream(output);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
gzip.write(buffer, 0, bytesRead);
}
input.close();
gzip.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
.class
file and display the first 20 bytes in hexadecimal format.GZIPCompression
example to verify reversibility using the CRC32
checksum.echo(Reader, Writer)
.