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
When writing HTML code, what is meant by 'syntax'?​
aleksandrvk [35]

Answer:

   Syntax is essentially the punctuation and grammar rules for a computer language.

Explanation:

  Certain characters and words have special meanings and must appear in a particular order for the computer code to make any sense. A simple example from line 3 in Figure 2.2 is the piece of HTML code <head>.

8 0
3 years ago
Which type of firewall policy calls for a firewall to deny all traffic by default?
charle [14.2K]
Restrictive policy is a kind of policy calls for a firewall to contradict all traffic by default. The first rule denies all traffic on any service and using any port. To permit a specific type of traffic, a new rule must be placed ahead of the deny all rule. A firewall should enforce the overall policy recognized by the network administrator. Enforcement is controlled mainly over setting up packet filtering rules which is a rule base comprises a set of these rules. 
3 0
3 years ago
What is the extension of excel worksheet​
muminat

Answer:

Excel file formats

Format Extension

Excel Workbook .xlsx

Excel Macro-Enabled Workbook (code) .xlsm

Excel Binary Workbook .xlsb

Template .xltx

7 0
2 years ago
Fica deduction consists of _____.
Marianna [84]

Answer

medicare and social security

Explanation

 FICA IS Federal Insurance Contributions Act. FICA tax is the money that is taken out of workers' paychecks to pay older Americans their Social Security retirement and Medicare that is Hospital Insurance benefits. It is a mandatory payroll deduction

7 0
3 years ago
Read 2 more answers
Which is an example of synchronous communication. Text message e-mail voicemail telephone conversation
enot [183]

Answer:

Text Messages

Explanation:

Synchronous communication is a simultaneous communication between two people. This is the type of serial communication where two person can communicate at the same time. This type of communication exclude face to face communication and phone communication.

for example

during Chat when we are taking to someone he also can reply the same time.

3 0
3 years ago
Read 2 more answers
Other questions:
  • Which term describes the distinct number of colors a graphic contains? (1 point)?
    10·1 answer
  • The strFirstName and strLastName variables contain the strings "Jane" and "Jones," respectively. Which of the following statemen
    14·1 answer
  • If there are no differences between the amino acid sequences in the cytochrome c protein of humans and chimpanzees why aren't we
    15·1 answer
  • Are there protections for people in terms of what data can be collected by different sites?
    10·1 answer
  • Which C99 function can be used to convert a string to a double?
    15·1 answer
  • To print a range of cells in the active worksheet, click___ in the settings in print gallery.
    7·1 answer
  • Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type t
    9·1 answer
  • Write a method that accepts a string as an argument and checks it for proper capitalization and punctuation. The method should d
    14·1 answer
  • GIVING OUT BRAINLIEST AND I AM ALSO WARNING EVERYONE TO NOT ANSWER A BUNCH OF rubbish just to get the points!
    7·2 answers
  • Why is a Quality assurance tester needed on a software development team?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!