You are on page 1of 4

1. What are the relevances of applying or using the Input Stream in Java?

Support your
answer by providing at least one (1) example.

In Java, the input stream is an abstract class that represents a sequence of ordered
bytes. It is the parent class of all classes that are capable of retrieving data from a
source like a file, a network connection, or a memory buffer. This class has general
methods for reading bytes. However, it is not possible to directly create an instance of
the abstract class. Instead, its subclasses, like FileInputStream, can be utilized to create
input streams for specific sources. This class is helpful for reading binary data from a
source consistently and efficiently. It is located in the java.io package and implements
the Closeable interface.

Example:
import java.io.FileInputStream;
import java.io.InputStream;

class example1 {
public static void main(String args[]) {

byte[] array = new byte[150];

try {
InputStream input = new FileInputStream("input.txt");

System.out.println("Available bytes in the file: " + input.available());

// Read byte from the input stream


input.read(array);
System.out.println("Data read from the file: ");

// Convert byte array into string


String data = new String(array);
System.out.println(data);

// Close the input stream


input.close();
} catch (Exception e) {
e.getStackTrace();
}
}
}

Content of input.txt
This is a line of text inside the file.
//this source code was copied from programiz.com
//as an example.

Output:

2. Differentiate the BufferedReader from Scanner Class in Java? Provide each example.
Scanner class can read different types of data and even Strings. It uses regular
expressions to do that. BufferedReader class can read text from a source like a file or a
keyboard. It stores some characters in memory to make reading efficient. A scanner can
do more things than BufferedReader. It can parse all types of data such as int, short,
byte and even strings from the user input. BufferedReader on the other hand, can only
read one data type, which is String. BufferedReader has a bigger memory than
Scanner, which means it is better for reading long words from a file and for reading
short input and data other than words, Scanner class can be used.
3. What are the common problems when using the JOptionPane? Provide at least three
(3) samples to justify your answer.

Here are some common syntax problems when using JOptionPane:


- Not putting arguments inside the method of showInputDialog. It is important to
take note that we need to add arguments inside the dialog as this serves as an
instruction for users as to what shall be inputted inside the input dialog.
- Incorrect parameters for showMessageDialog. Keep in mind that two parameters
are needed for showMessageDialog. The first one is null, and the second one is
the message shown inside the dialog.

- Not importing the JOptionPane. Although very simple, it is still common to forget
importing packages in the program. This is important for the machine to know
where they will be getting the commands that are being used inside the program.

References:
GeeksforGeeks. (2022, September 21). Difference between scanner and Bufferedreader class
in Java. GeeksforGeeks. Retrieved March 29, 2023, from
https://www.geeksforgeeks.org/difference-between-scanner-and-bufferreader-class-in-java/

Java InputStream class. Programiz. (n.d.). Retrieved March 29, 2023, from
https://www.programiz.com/java-programming/inputstream

Reading keyboard output. SlideShare. (2014, December 5). Retrieved March 29, 2023, from
https://www.slideshare.net/shaylor_swift/midterm-slide2

You might also like