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

Byte Streams and Character Streams, and Reading and Writing Files in Java

Uploaded by

sainiaditya246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Byte Streams and Character Streams, and Reading and Writing Files in Java

Uploaded by

sainiaditya246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question: 1 Explain the difference between Byte Streams and Character

Streams in Java. When would you use each type of stream?

Answer: Byte Streams and Character Streams are two types of streams
used for input and output operations in Java. The primary difference
between them lies in the way they handle data.

Byte Streams:

 Byte Streams deal with binary data, which is a sequence of 8-bit


bytes.
 They are used to read and write binary data from/to files, network
connections, or any other source/destination.
 Byte Streams are suitable for handling any type of data, including
non-character data such as images, audio, or video files.
 They are less efficient for handling character data because they
treat characters as raw binary values without considering character
encoding.

Character Streams:

 Character Streams handle character data, which is represented


using Unicode encoding.
 They are more efficient for handling character data, as they
automatically handle character encoding and decoding.
 Character Streams are suitable for reading and writing text data,
such as files containing text, XML, or source code.
 They are less efficient for handling binary data, as they need to
convert the binary data into characters, which can lead to data
corruption or loss of information.
When to use Byte Streams:

 When working with binary data, such as reading or writing image


files, audio files, or any other non-text data.
 When dealing with raw byte data that does not require character
encoding or decoding.

When to use Character Streams:

 When working with character data, such as reading or writing text


files, XML files, or source code files.
 When handling data that requires character encoding or decoding,
such as Unicode text.

// Reading Files in Java using Byte and Character Streams


import java.io.*;

public class FileReadExample {


public static void main(String[] args) {
// Reading a binary file (Byte Stream)
try (FileInputStream fis = new FileInputStream("image.jpg")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
// Process the read bytes
// For example, you can write them to another file
// OutputStream os = new FileOutputStream("new_image.jpg",
true);
// os.write(buffer, 0, bytesRead);
// os.close();
}
} catch (IOException e) {
e.printStackTrace();
}

// Reading a text file (Character Stream)


try (BufferedReader br = new BufferedReader(new
FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Question: Write a Java program that reads the contents of a binary file
(e.g., an image or audio file) using a Byte Stream and displays the
number of bytes read.
Answer:

You might also like