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

Videos

Textbook Question
Book Icon
Chapter 10, Problem 1E

Write a program that will write the Gettysburg Address to a text file. Place each sentence on a separate line of the file.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program plan:

  • Include the required header files.
  • Create a class “Gettysburg”.
    • Define main function.
      • Declare a variable “filename” with the value as file name.
      • In “try” block,
        • Create an object for “PrintWriter” class and pass the parameter as “filename”.
        • Write the contents into the file.
        • Close the file.
      • In “catch” block,
        • If the file not found in the folder, throw a “FileNotFoundException”.
      • Display the statement.

Explanation of Solution

 Program:

//import the header file

import java.io.*;

//definition of "Gettysburg" class

public class Gettysburg

{

    //definition of main method

    public static void main(String[] args)

    {

        //declare the variable and assign the filename

        String filename = "gettysburg.txt";

        //try block

        try

        {

/*create object for "PrintWriter" class and pass the file name*/

PrintWriter outputStream = new PrintWriter(filename);

            //write the contents to the file

outputStream.println("Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.");

outputStream.println("Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure.");

outputStream.println("We are met on a great battle-field of that war.");

outputStream.println("We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.");

outputStream.println("It is altogether fitting and proper that we should do this.");

outputStream.println("But, in a larger sense, we can not dedicate-we can not consecrate-we can not hallow-this ground.");

outputStream.println("The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.");

outputStream.println("The world will little note, nor long remember what we say here, but it can never forget what they did here.");

outputStream.println("It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. ");

outputStream.println("It is rather for us to be here dedicated to the great task remaining before us - that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion - that we here highly resolve that these dead shall not have died in vain - that this nation, under God, shall have a new birth of freedom - and that government of the people, by the people, for the people, shall not perish from the earth.");

            //close the file

            outputStream.close();

        }

        //catch block

        catch(FileNotFoundException e)

        {

            //display the error message

System.out.println("Error opening the file " + filename);

            //exit the program

            System.exit(0);

        }

        //display the message

System.out.println("The file gettysburg.txt has been written.");

    }

}

Sample Output

Output:

The file gettysburg.txt has been written.

Output file:

Screenshot of “gettysburg.txt” text file

Java: An Introduction to Problem Solving and Programming (7th Edition), Chapter 10, Problem 1E

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a program that reads every line in a text file, removes the first wordfrom each line, and then writes the resulting lines to a new text file.
Write a program that reads the contents of a text file and prints the letter that starts the mostunique words in that file.
Write a program that removes all the occurrences of a specified string from a text file.Removes the string from the specified file. Your program should get the arguments from the command line.

Chapter 10 Solutions

Java: An Introduction to Problem Solving and Programming (7th 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.6 - Prob. 36STQCh. 10.6 - Prob. 37STQCh. 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. 9PPCh. 10 - Prob. 12PPCh. 10 - Prob. 15PP
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
    Systems Architecture
    Computer Science
    ISBN:9781305080195
    Author:Stephen D. Burd
    Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Files & File Systems: Crash Course Computer Science #20; Author: CrashCourse;https://www.youtube.com/watch?v=KN8YgJnShPM;License: Standard YouTube License, CC-BY
UNIX Programming (Part - 10) The File System (Directories and Files Names); Author: ITUTEES;https://www.youtube.com/watch?v=K35faWBhzrw;License: Standard Youtube License