Question

4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

4.3Learning Objective: To read and write text files.

Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43).

Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output the contents to a new file named the same as the input file, but with a .txt file name extension, e.g., if the input file is foo.java then the output file shall be named foo.java.txt. Each line of the input file shall be numbered with a line number (formatted as shown below) in the output file. For example, if the user enters H01_43.java as the name of the input file and H01_43.java contains:

//***************************************************************

// CLASS: H01_43

//***************************************************************

public class H01_43 {

public static void main(String[] pArgs) {

}

private run() {

}

}

then the contents of the output file H01_43.java.txt would be:

[001] //***************************************************************

[002] // CLASS: H01_43

[003] //***************************************************************

[004] public class H01_43 {

[005] public static void main(String[] pArgs) {

[006] }

[007] private run() {

[008] }

[009] }

You may assume that the file being read exists.

Hint: To print an integer as shown above in a field of width 3 with leading 0's use the Java API PrintWriter.printf() method with a format specifier of %03d. The % symbol tells the compiler this is a format specifier, 3 is the field width for the integer line number, the 0 preceding 3 tells the compiler to output leading 0's rather than leading spaces if the line number does not take up the entire field width, and d tells the compiler that we are printing an integer).

Testing: Make sure to test the case where the input file exists but is empty.

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

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class CopyFile {
   public static void main(String[] args) throws Exception {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter file name");
       String name = sc.nextLine();
       BufferedReader br = null;
       try {
           br = new BufferedReader(new FileReader(name));
       } catch (FileNotFoundException e) {
           e.printStackTrace();
           return;
       }
       String line = br.readLine();
       int lineCount = 1;
       PrintWriter pw = new PrintWriter(new FileWriter(name + ".txt"));
       while (line != null) {
           pw.printf("[%03d]", lineCount++);
           pw.println(line);
           line=br.readLine();
       }
       pw.flush();
       pw.close();
       br.close();

   }
}

Add a comment
Know the answer?
Add Answer to:
4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...
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
  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • *Java* Given the attached Question class and quiz text, write a driver program to input the...

    *Java* Given the attached Question class and quiz text, write a driver program to input the questions from the text file, print each quiz question, input the character for the answer, and, after all questions, report the results. Write a Quiz class for the driver, with a main method. There should be a Scanner field for the input file, a field for the array of Questions (initialized to 100 possible questions), and an int field for the actual number of...

  • Q11: Java source files should be stored in text files with java extension. Also, name of...

    Q11: Java source files should be stored in text files with java extension. Also, name of the file must match with the class name in the file. What is the name of the compiled version of the source file Monster.java ? a. Monster.java b. Monster.bytecode c. Monster.class d. Monster.compiled

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

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
ADVERTISEMENT