Question

In a class named InputTextToOutputFile.java use the following prompt to get the fileName of the output...

In a class named InputTextToOutputFile.java use the following prompt to get the fileName of the output file from the user: “What is the name of your output file?”

Once the output file is opened, write everything the user types until the input contains “STOP!” Note: Include the line containing “STOP!” as the last thing written to the file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class InputTextToOutputFile
{
public static void main(String[] args) throws IOException
{
Scanner in=new Scanner(System.in);//Scanner object to get user input
  
   System.out.println("What is the name of your output file? ");
  
   String fileName = in.nextLine();//get output file name
  
   File file = new File(fileName);//File object with fileName as input
  
   //Create the file by method file.createNewFile()
   if (file.createNewFile())
   {
   System.out.println("File is created!");
   } else {
   System.out.println("File already exists.");
   }
  
   //FileWriter object with user file name given as input
   FileWriter writer = new FileWriter(file);
   System.out.println("Enter text to write to a file : ");
   String line;//variable to store line content
   do {
       line=in.nextLine();//get line content from user
       writer.write(line+"\n"); //write content to file by adding new line(\n) character
   }while(!line.equals("STOP!"));//repeat a loop until user enters "STOP!" line
  
   writer.close();//close the file object
}
}

Sample Input & Output:

Add a comment
Know the answer?
Add Answer to:
In a class named InputTextToOutputFile.java use the following prompt to get the fileName of the output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • Write a python program: using class named Example. it will accept a name, the content of...

    Write a python program: using class named Example. it will accept a name, the content of the file. we will use few functions to return the name of the file, owner, set date (time it was created the file), we can also add a line and delete a line from the file. another function that returns all the data in the file. and one that deletes any previous and sets any new data user enters. we can do like Test="a","this...

  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • on a mac!!!!!! Write a script named copyfile.py. This script should prompt the user for the...

    on a mac!!!!!! Write a script named copyfile.py. This script should prompt the user for the names of two text files. The contents of the first file should be input and written to the second file.

  • Description: Save a text document to disk based on a name and content provided by the...

    Description: Save a text document to disk based on a name and content provided by the user. Purpose: This application provides experience with user input and interaction in the Console, writing files to disk, working with exceptions, and writing programs in C#/.NET. Requirements: Project Name: Document Target Platform: Console Programming Language: C# Documentation: Types and variables (Links to an external site.) (Microsoft) Console.ReadLine Method () (Links to an external site.) (Microsoft) Strings (C# Programming Guide) (Links to an external site.)...

  • C++ Visul Studio Create a class named Vehicle. The class has the following five member variables:...

    C++ Visul Studio Create a class named Vehicle. The class has the following five member variables: • Vehicle Name • Vehicle number • Sale Tax • Unit price • Total price Include set (mutator) and get (accessor) functions for each field except the total price field. The set function prompt the user for values for each field. This class also needs a function named computePrice() to compute the total price (quantity times unit price + salesTax) and a function to...

  • FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named...

    FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named TextBookSort.java. Include these steps: Create a class titled TextBook that contains fields for the author, title, page count, ISBN, and price. This TextBook class will also provide setter and getter methods for all fields. Save this class in a file titled TextBook.java. Create a class titled TextBookSort with an array that holds 5 instances of the TextBook class, filled without prompting the user for...

  • Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state...

    Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state codes and state names. The file has four columns and they are separated by commas. The first column is the state full name and the second column is the state code. You don't have to worry about column 3 & 4. You should eliminate any row that is without a state code. Save the two columns into a dictionary with key = state code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT