0% found this document useful (0 votes)
15 views

Java unit

Java programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java unit

Java programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java FileInputStream Class

The FileInputStream class in Java is a part of the Java I/O (Input/Output) package (java.io). It is used to read
binary data from files, which means it's suited for reading raw bytes, such as images, audio, video, etc. This class
extends the InputStream class.

Here's a brief overview of the FileInputStream class and how you can use it to read data from a file:

1. Creating a FileInputStream object: To use FileInputStream, you first need to create an instance of it by
providing the path of the file you want to read.

java Copy code


FileInputStream fis = new FileInputStream("path/to/your/file.txt");

2. Reading from the file: You can use the read method of FileInputStream to read a byte of data. It returns -1
when the end of the file is reached.

java Copy code


int byteData;
while ((byteData = fis.read()) != -1) {
// Process the byteData
// byteData will contain the next byte read from the file
}

3. Reading into a byte array: You can also read a specific number of bytes into a byte array using the read
method with a byte array as an argument.

java Copy code


byte[] buffer = new byte[1024]; // You can choose the buffer size
int bytesRead;

while ((bytesRead = fis.read(buffer)) != -1) {


// Process the bytesRead number of bytes in the buffer
}

4. Closing the FileInputStream: It's essential to close the FileInputStream after you're done using it to release
the associated resources.

java Copy code


fis.close();

Example usage:

java Copy code


import java.io.FileInputStream;
import java.io.IOException;

public class FileReadExample {


public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("path/to/your/file.txt");
int byteData;
while ((byteData = fis.read()) != -1) {
// Process the byteData (e.g., print it as a character)
System.out.print((char) byteData);
}
fis.close();
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}

In this example, the program reads a file character by character and prints each character to the console.

Java fileoutputsteam class

The FileOutputStream class in Java is a part of the Java I/O (Input/Output) package (java.io). It is used to write
binary data to a file. This class extends the OutputStream class.

Here's a brief overview of the FileOutputStream class and how you can use it to write data to a file:

1. Creating a FileOutputStream object: To use FileOutputStream, you first need to create an instance of it by
providing the path of the file you want to write to.

java Copy code


FileOutputStream fos = new FileOutputStream("path/to/your/file.txt");

You can also pass a boolean parameter to the constructor to indicate whether to append to an existing file
(true) or overwrite it (false).

java Copy code


FileOutputStream fos = new FileOutputStream("path/to/your/file.txt", true); // to append

2. Writing to the file: You can use the write method of FileOutputStream to write a byte of data.

java Copy code


byte[] data = "Hello, World!".getBytes();
fos.write(data);

3. Writing from a byte array: You can write data from a byte array to the file using the write method.

java Copy code


byte[] data = {65, 66, 67}; // ASCII values for 'A', 'B', 'C'
fos.write(data);

4. Closing the FileOutputStream: It's essential to close the FileOutputStream after you're done using it to
release the associated resources and ensure that any buffered data is flushed to the file.

java Copy code


fos.close();

Example usage:

java Copy code


import java.io.FileOutputStream;
import java.io.IOException;

public class FileWriteExample {


public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("path/to/your/file.txt");
String data = "Hello, World!";
fos.write(data.getBytes());
fos.close();
System.out.println("Data written to the file.");
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}

In this example, the program writes the string "Hello, World!" to the specified file. Make sure to handle exceptions
appropriately, especially when working with file I/O operations.

ByteArrayOutputStream

The ByteArrayOutputStream class in Java is a part of the Java I/O (Input/Output) package (java.io). It's a subclass
of OutputStream and provides a way to write data to a byte array in memory.

Here's a brief overview of the ByteArrayOutputStream class and how you can use it to write data to a byte array:

1. Creating a ByteArrayOutputStream object: You create an instance of ByteArrayOutputStream without


specifying a file path, as it's designed to write to an in-memory byte array.

java Copy code


ByteArrayOutputStream baos = new ByteArrayOutputStream();

2. Writing to the byte array: You can use the write method of ByteArrayOutputStream to write a byte or a
byte array to the in-memory byte array.

java Copy code


byte[] data = {65, 66, 67}; // ASCII values for 'A', 'B', 'C'
baos.write(data);

// You can also write a single byte


baos.write(68); // ASCII value for 'D'

3. Getting the byte array: To obtain the byte array containing the data written to the ByteArrayOutputStream,
you can use the toByteArray method.

java Copy code


byte[] byteArray = baos.toByteArray();

4. Closing the ByteArrayOutputStream: Unlike file-based output streams, ByteArrayOutputStream doesn't


require closing since it works with an in-memory byte array.
Example usage:

java Copy code


import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamExample {


public static void main(String[] args) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] data = {65, 66, 67}; // ASCII values for 'A', 'B', 'C'
baos.write(data);

// Write a single byte


baos.write(68); // ASCII value for 'D'

// Get the byte array


byte[] byteArray = baos.toByteArray();

// Print the contents of the byte array


for (byte b : byteArray) {
System.out.print((char) b);
}

// Note: There's no need to explicitly close ByteArrayOutputStream


} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}

In this example, we write bytes to the ByteArrayOutputStream, convert it to a byte array, and then print the
characters corresponding to the byte values in the array.

ByteArrayInputStream

The ByteArrayInputStream class in Java is a subclass of InputStream that reads bytes from an in-memory byte
array. It allows you to read data from a byte array as if it were an input stream.

Here's a brief overview of the ByteArrayInputStream class and how you can use it to read data from a byte array:

1. Creating a ByteArrayInputStream object: You create an instance of ByteArrayInputStream by providing


a byte array to read from.

java Copy code


ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);

Here, byteArray is the byte array from which you want to read.

2. Reading from the byte array: You can use the read method of ByteArrayInputStream to read a byte of
data. It returns -1 when the end of the byte array is reached.
java Copy code
int byteData;
while ((byteData = bais.read()) != -1) {
// Process the byteData
// byteData will contain the next byte read from the byte array
}

3. Reading into a byte array: You can read a specific number of bytes into a byte array using the read method
with a byte array as an argument.

java Copy code


byte[] buffer = new byte[1024]; // You can choose the buffer size
int bytesRead;

while ((bytesRead = bais.read(buffer)) != -1) {


// Process the bytesRead number of bytes in the buffer
}

4. Marking and Resetting: ByteArrayInputStream supports marking a position in the stream and later
resetting to that position using the mark and reset methods.

Example usage:

java Copy code


import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamExample {


public static void main(String[] args) {
byte[] byteArray = {65, 66, 67, 68, 69}; // ASCII values for 'A' to 'E'

try {
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);

int byteData;
while ((byteData = bais.read()) != -1) {
// Process the byteData (e.g., print it as a character)
System.out.print((char) byteData);
}
System.out.println(); // Newline for formatting

// Reset the stream to the beginning


bais.reset();

// Read into a byte array


byte[] buffer = new byte[3]; // Read 3 bytes at a time
int bytesRead;
while ((bytesRead = bais.read(buffer)) != -1) {
// Process the bytesRead number of bytes in the buffer
for (int i = 0; i < bytesRead; i++) {
System.out.print((char) buffer[i]);
}
}

bais.close(); // Close the ByteArrayInputStream


} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}

In this example, we read bytes from a byte array using ByteArrayInputStream and demonstrate both single-byte
reading and reading into a buffer. We also reset the stream to reread the byte array.
PipedWriter

PipedWriter is a class in Java that is used to write character data to a connected PipedReader. It is typically used
for communication between two threads in the same JVM (Java Virtual Machine) or between different parts of a
program.

Here's a brief overview of the PipedWriter class and how you can use it:

1. Creating a PipedWriter object: You create an instance of PipedWriter by providing a connected


PipedReader object.

java Copy code


PipedReader reader = new PipedReader();
PipedWriter writer = new PipedWriter(reader);

Here, reader is a PipedReader object that is connected to this writer.

2. Writing to the piped stream: You can use the write method of PipedWriter to write character data to the
connected PipedReader.

java Copy code


char[] charData = {'H', 'e', 'l', 'l', 'o'};
writer.write(charData);

3. Closing the PipedWriter: It's essential to close the PipedWriter after you're done using it to release the
associated resources.

java Copy code


writer.close();

Example usage:

java Copy code


import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedWriterExample {


public static void main(String[] args) {
try {
PipedReader reader = new PipedReader();
PipedWriter writer = new PipedWriter(reader);

String message = "Hello, PipedReader!";


writer.write(message);

writer.close(); // Close the PipedWriter

// Read from the connected PipedReader


int data;
while ((data = reader.read()) != -1) {
System.out.print((char) data);
}
reader.close(); // Close the PipedReader
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}

In this example, we create a PipedWriter connected to a PipedReader. We write a message to the PipedWriter,
close it, and then read the message from the connected PipedReader. The characters are then printed to the console.
Note that the order of closing the writer and reader is important to avoid blocking issues.

PipedReader

PipedReader is a class in Java that is used to read character data from a connected PipedWriter. It allows for
communication between two threads or parts of a program by using piped streams.

Here's a brief overview of the PipedReader class and how you can use it:

1. Creating a PipedReader object: You create an instance of PipedReader and connect it to a PipedWriter.

java Copy code


PipedReader reader = new PipedReader();
PipedWriter writer = new PipedWriter(reader);

The writer is the PipedWriter to which this PipedReader is connected.

2. Reading from the piped stream: You can use the read method of PipedReader to read character data from
the connected PipedWriter.

java Copy code


int charData = reader.read(); // Read a single character

Alternatively, you can read into a character array for more efficient reading.

java Copy code


char[] buffer = new char[1024]; // Buffer to hold characters
int bytesRead = reader.read(buffer); // Read into buffer

3. Closing the PipedReader: It's essential to close the PipedReader after you're done using it to release the
associated resources.

java Copy code


reader.close();

Example usage:

java Copy code


import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
public class PipedReaderExample {
public static void main(String[] args) {
try {
PipedReader reader = new PipedReader();
PipedWriter writer = new PipedWriter(reader);

String message = "Hello, PipedWriter!";


writer.write(message);

// Close the PipedWriter to indicate the end of writing


writer.close();

// Read from the connected PipedReader


char[] buffer = new char[1024]; // Buffer to hold characters
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
// Process the bytesRead number of characters in the buffer
System.out.print(new String(buffer, 0, bytesRead));
}

// Close the PipedReader


reader.close();
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}

In this example, we create a PipedReader and connect it to a PipedWriter. We then write a message using the
PipedWriter, close it, and read the message from the connected PipedReader. The characters are then printed to the
console.

Regenerate

You might also like