Write a
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.
- Define main function.
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.");
}
}
Output:
The file gettysburg.txt has been written.
Output file:
Screenshot of “gettysburg.txt” text file
Want to see more full solutions like this?
Chapter 10 Solutions
Java
Additional Engineering Textbook Solutions
Absolute Java (6th Edition)
Starting Out with Programming Logic and Design (4th Edition)
Starting Out with C++: Early Objects (9th Edition)
C How to Program (8th Edition)
Starting Out with C++: Early Objects
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Write a program that will read a message from another text file and surround each occurrence of an abbreviation with <> brackets.arrow_forwardWrite a program that prompts the user for an input file name, reads all words from the input file, and writes the words to the output file sentences.txt. Start a new line whenever a word ends in a period, question mark, or exclamation mark. C++arrow_forwardWrite a program to read all the values in the attached file test_scores.csv, calculate the total of the values, average of all values, then display them on screen.arrow_forward
- Write a program that removes all the occurrences of a specifiedstring from a text file. For example, invokingjava Exercise12_11 John filenameremoves the string John from the specified file. Your program should get thearguments from the command line.arrow_forwardWrite a program that prompts the user for an input file name, reads all words from the input file, and writes the words to the output file named sentences.txt. Start a new line whenever a word ends in a period, question mark, or exclamation mark.arrow_forwardWrite a program that reads data from a file containing integers that ends with -999. Output the numbers that are divisible by 7 Output the numbers that are divisible by 11 Output the numbers that are not divisible by 7 or 11arrow_forward
- Write a program to read two file names from the command line. If the number of arguments in the command line are more than or less than two, then the program should terminate with an appropriate error message. Otherwise, it should open the first file and copy its contents into the second file, in effect overwriting its contents, if any.arrow_forwardWrite a program to read a text file line by line and display each word separatedby '#'.arrow_forwardWrite a program FileSorter that takes the name of adirectory as a command-line argument and prints out all of the files in the currentdirectory, sorted by file name. Hint : Use the File data type.arrow_forward
- Writes the contents of a file one page at a time, using the command write-page. O a. Less O b. More O a. Less O c. Head O d. Tail O c. Head O d. Tailarrow_forwardWrite a python program that asks the user how many words they would liketo write to a file, and then asks the user to enter that many words,one at a time. The words should be written to a file.arrow_forwardThe Problem: People keep wanting to extend programs by adding morefeatures. If the program deals with binary files the file format mustaccommodate this.arrow_forward
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning