Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 10.4, Problem 26STQ

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...

Blurred answer
Students have asked these similar questions
Discuss some common exceptions that can occur when working with files in programming and how they can be handled effectively.
Given an initialised variable fileName, write a sequence of statements that creates a file with the name specified by fileName and a single line containing "This Is File: " followed by the file's name. Verify that the data written to the file has been purged from its buffer and that all system resources consumed by these statements have been released. (Do not worry about any potential exceptions here; assume that they are handled elsewhere.)
What are common file-related exceptions in programming, and how can they be caught and handled effectively?

Chapter 10 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 10.3 - Prob. 11STQCh. 10.4 - Write some Java code to create an output stream of...Ch. 10.4 - Give three statements that will write the values...Ch. 10.4 - Give a statement that will close the stream toFile...Ch. 10.4 - What import statement(s) do you use when creating...Ch. 10.4 - Prob. 16STQCh. 10.4 - Give three statements that will read three numbers...Ch. 10.4 - Give a statement that will close the stream...Ch. 10.4 - Can you use writeInt to write a number to a file...Ch. 10.4 - Can you use readUTF to read a string from a text...Ch. 10.4 - Prob. 21STQCh. 10.4 - Prob. 22STQCh. 10.4 - Does the class FileInputStream have a method named...Ch. 10.4 - Does the class FileOutputStream have a constructor...Ch. 10.4 - Does the class ObjectOutputStream have a...Ch. 10.4 - Prob. 26STQCh. 10.4 - Suppose that a binary file contains exactly three...Ch. 10.4 - The following code appears in the program in...Ch. 10.4 - Prob. 29STQCh. 10.5 - Prob. 30STQCh. 10.5 - Prob. 31STQCh. 10.5 - Prob. 32STQCh. 10.5 - Prob. 33STQCh. 10.6 - Prob. 34STQCh. 10.6 - Prob. 35STQCh. 10 - Write a program that will write the Gettysburg...Ch. 10 - Modify the program in the previous exercise so...Ch. 10 - Write some code that asks the user to enter either...Ch. 10 - Write a program that will record the purchases...Ch. 10 - Modify the class LapTimer, as described in...Ch. 10 - Write a class TelephoneNumber that will hold a...Ch. 10 - Write a class contactInfo to store contact...Ch. 10 - Write a program that reads every line in a text...Ch. 10 - Repeat the previous exercise, but write the new...Ch. 10 - Write a program that will make a copy of a text...Ch. 10 - Suppose you are given a text file that contains...Ch. 10 - Suppose that you have a binary file that contains...Ch. 10 - Suppose that we want to store digitized audio...Ch. 10 - Write a program RecoverSignal that will read the...Ch. 10 - Even though a binary file is not a text file, it...Ch. 10 - Write a program that searches a file of numbers...Ch. 10 - Write a program that reads a file of numbers of...Ch. 10 - The following is an old word puzzle: Name a common...Ch. 10 - The Social Security Administration maintains an...Ch. 10 - The following is a list of scores for a game....Ch. 10 - Write a program that checks a text file for...Ch. 10 - Prob. 5PPCh. 10 - Prob. 6PPCh. 10 - Revise the class Pet, as shown in Listing 6.1 of...Ch. 10 - Write a program that reads records of type Pet...Ch. 10 - Prob. 12PP
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr