Concept explainers
Explanation of Solution
Exception thrown when opening a binary file for output:
When opening a binary file for output, the “FileNotFoundException” and “IOException” will be thrown.
- A “FileNotFoundException” would be thrown if the file could not be opened because, there is already a folder named which was given by the user.
- This exception can be thrown when an application tries to open a file for writing, but that file only for read, or the permissions of the file don’t allow the file to be read by any application.
- An “IOException” is thrown when there is any input/ output file operation issues while application performing particular tasks accessing the files.
- A “FileNotFoundException” is one of the sub class of an “IOException”.
Example:
Consider an example for the above snippet,
//include requried header files
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.util.Scanner;
//definition of "ReadFile" class
public class ReadFile
{
//definition of main method
public static void main(String[] args)throws Exception
{
//declare and set the value to the variable
String filename = null;
//try block
try
{
//create an object for scanner class
Scanner x = new Scanner (System.in);
//get the file name from the user
System.out.println("Enter the file name: ");
filename = x.next();
/*create an object for "ObjectInputStream" stream class*/
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filename));
//display the statement
System.out.println("The first thing in the file " + filename + " is");
//read the input string
String f = inputStream...
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- What are the benefits of exception handling?arrow_forwardHow can the standard error message be shown when an exception is thrown?arrow_forwardPart 1: Encryption A. Using your FileReader, read the file "PlentyOfRoomAtTheBottom.txt" in the Labs Module. There are non-printable characters in the file. Sometimes FileReaders abort when encountering a non-printable or extended ASCII characters. Handle this with exception handling if it happens. Your program should continue processing instead of aborting. Assign a specific character, of your choice, to the aborted character. B. Using the rawdata from reading the "PlentyOfRoomAtTheBottom.txt", develop a frequency counter algorithm for each character the lower ASCII table. For example: Character[ a ] = Frequency[ 2475 ] Character[ b ] = Frequency[ 420 ] Character[ c ] = Frequency[ 985 ] C. Sort the Lower ASCII frequency characters (i.e. 1 to 128) in decending order and process the data by using your QTree algorithm. Your QTree algorithm should be based on a PRIORITY_QUEUE. D. Create bit-trails, as defined in the Exercise Bit-Trail, for each character your QTree. E.…arrow_forward
- The Third program needs to handle data from files (open, read/write, delete to handle data). It uses exception handling – try and except to catch and handle exceptions. Follow coding standards, naming conventions for variables and functions. Students will be asked to explain the working of their program and the logic they’ve used.arrow_forwardException Project JAVA Create a Java program that searches for a student’s ID or name in a text file, complete the findID() and findName() functions. Then, insert a try/catch statement in main() to catch any exceptions thrown by findID() or findName(), and output the exception message. Each line in the text file contains a name and ID separated by a space. Function findID() has two parameters: a student's name (string) and the text file's contents (infile, input file variable). The function findID() returns the ID associated with the student's name if the name is in the file, otherwise the function throws a runtime error with the message "Student ID not found for studentName", where studentName is the name of the student. Function findName() has two parameters: a student's ID (string) and the text file's contents (infile). The function findName() returns the name associated with the student's ID if the ID is in the file, otherwise the function throws a runtime error with the message…arrow_forwardWhat kinds of exceptions are captured during compilation?arrow_forward
- To show a standard error message when an exception occurs, how do you do it?arrow_forwardin pytrhon programming language, What happens when an unhandled exception occurs? The finally clause executes, and the code finishes. The finally clause executes, and the code returns -1. The finally clause executes, and then the exception is re-raised. The finally clause executes, and then runs the try block again.arrow_forwardAnswer the following questions based on your knowledge of java exception and java text and binary I/Os: Java exception can be handled locally using try-catch block or can be thrown to the calling When should you prefer to throw an exception to the calling method instead of using try-catch? Is it possible to write a program to read from a text file without handling the exception? Why should a programmer consider binary I/O instead of text I/O? Suppose that you are implementing a program to write a string “সিএিই-110” in a binary file. Java provides you three different methods – writeBytes(String), writeChars(String), and writeUTF(String). How many bytes will be written by each of those method for the give string? Why should not you consider the writeBytes(String) method in this case?arrow_forward
- JAVA CODE Unchecked exceptions are exceptions that are usually the result of a mistake and we aren't expected to handle, while checked exceptions are exceptions that we reasonably expect to occur that we should handle whenever possible. Name one type of unchecked exception and one type of checked exception, and give an example of situations that might cause those exceptions to be thrown.arrow_forwardConsider this scenario: An exception is thrown when a function is called. What are the three things that the function can do?arrow_forwardPlease give the names of the various approaches of managing exceptions that are available.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr