1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
finlep [7]
3 years ago
8

Write a program that reads a file containing text. Read each line and send it to the output file, preceded by line numbers. If t

he input file is Mary had a little lamb Whose fleece was white as snow. And everywhere that Mary went, The lamb was sure to go! then the program produces the output file /* 1 */ Mary had a little lamb /* 2 */ Whose fleece was white as snow. /* 3 */ And everywhere that Mary went, /* 4 */ The lamb was sure to go! The line numbers are enclosed in /* */ delimiters so that the program can be used for numbering Java source files. Prompt the user for the input and output file names.

Computers and Technology
1 answer:
Shtirlitz [24]3 years ago
7 0

Answer:

I am writing a JAVA program:

import java.io.FileReader;  // class used for reading data from a file

import java.io.IOException;  //class used for handling exceptions

import java.io.PrintWriter;  // class used to printing or writing formatted data. It is used when the data to be written contains text and numbers

import java.util.Scanner;   // class used to take input from the user

public class Main  {  //start of the class

public static void main(String[] args)  {//start of main() function body

Scanner input = new Scanner(System.in);  //creates Scanner class object

System.out.print("Enter the input file name: ");  // prompts user to enter the input file name

String input_file = input.next();  //to  read and return the token i.e. input file name and store this into input_file String type variable

System.out.print("Enter the output file name: "); // prompts user to enter the output file name

String output_file = input.next();   /to  read and return the token i.e. output file name and store this into output_file String type variable

try   {/* try  defines a chunk of code to be tested for errors during the execute of this chunk. its a code block for reading from input file and writing to output file. This handles the exception if program cannot create or write to the file indicated. */

   FileReader inFile = new FileReader(input_file);  //creates object inFile of FileReader class to read the input_file contents

   Scanner scan = new Scanner(inFile);  //read and scans the input file

   PrintWriter outFile = new PrintWriter(output_file);  //creates object outFile of PrintWriter class to write to the output file. It is used to write output data in a readable text form.

   int count = 1;    //counts the line number      

   while (scan.hasNextLine())  {  //loop keeps checking for the next line in input file

    String lines = scan.nextLine();  // reads the entire line of the input file and stores that line contents in String lines

    outFile.println("/* " + count + " */ " + lines);  //each line represented with line number enclosed in /* */

    count++;  }   //increments the line number at each iteration    

    outFile.close();        }  //closes the output file after writing the contents from input file with each line of the input file enclosed in  /* */

catch (IOException e)  {  // catching the IOException and prints the following message if program cannot create input_file or write to the output_file

   System.out.println("Error processing file: " + e);    }     }   }

Explanation:

The program is well explained in the comments mentioned with each statement of the above program. The user is prompted to enter input and output file names. Then the program uses a while loop that scans through lines of the input file and count variable counts each line of input file. Then at each iteration the count variable counts a line of input file and line number of each line of the input file is enclosed in /* */ delimiters. The contents of input file, in which the line numbers of text lines of the input file are enclosed in /* */ delimiters, is written to the output file.

The screenshot of the program is attached along with the output file produces as a result of this program execution.

You might be interested in
A=3<br> b = 2<br> print (a ** b)<br> What is output?
Lady bird [3.3K]

Answer: ( 3**2)

Explanation: honestly I can't help you. Try google it is waiting for you with open harms.  

6 0
2 years ago
Read 2 more answers
An online retailer has developed an algorithm to provide personalized recommendations to shoppers. Which form of IPR protects th
Helen [10]

Answer:

should be B.

Explanation:

If not then Im sorry

5 0
2 years ago
You should check your battery ___________. Every week Never Every six months Every 30,00 miles
vova2212 [387]

The answer is Every six months

A battery acts as the brain of a car and is used to power almost everything. It can last up to five years. However, this lifespan will depend on how well the battery is maintained and the type of weather conditions you live in. Aim to check your car battery at least twice a year. Actually, it is best to check your battery regularly and not to wait for your mechanic to test your battery during routine servicing.

5 0
3 years ago
Read 2 more answers
What are several networks connected together using a router
elixir [45]

Answer:

local Area Network, Wide Local Area Network and Personal Area Network

8 0
2 years ago
The background image does not affect any cell’s format or content. _______
Korvikt [17]
Hmmm...that is true 
Here a quizlet about if you need it!
https://quizlet.com/15207805/csci-241-flash-cards/
3 0
2 years ago
Other questions:
  • show how one version of the technology is an improvement over a previous iteration of that same technology
    11·1 answer
  • Rita wants to know the size of each image in a folder. Which view will help her find this information quickly?
    7·1 answer
  • Today encoding scheme has taken over ascII by what
    5·1 answer
  • Which of the following does every font that you choose communicate, either on a conscious or subconscious level?
    10·2 answers
  • A line beginning with a # will be transmitted to the programmer’s social media feed.
    11·2 answers
  • Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integer
    11·1 answer
  • Excel files have a default extension of ?
    12·1 answer
  • All of the following are true about data science and big data except: a.Digital data is growing at the rate of 2.5 quintillion b
    15·1 answer
  • A type of user interface that features on- screen objects, such a menus and icons, manipulated by a mouse.
    7·1 answer
  • Where can i make a 3d animation for free ( pls don't trick me)
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!