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)
- Answer the following questions based on knowledge of java exception and java text and binary I/Os: Please answer according to question please please.arrow_forwardIs there any way to skip Finally block of exception even if some exception occurs in the exception block?arrow_forwardjava programming language note: this is only one question, not threearrow_forward
- Solve this according to exception handling and file management of Java prograam.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_forwardTo show a standard error message when an exception occurs, how do you do it?arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr