Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4.3 Name your source code file Hw1_1.java . Write a program that prompts the use

ID: 2247024 • Letter: 4

Question

4.3 Name your source code file Hw1_1.java . Write a program that prompts the user for the name of a Java source code

file. 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 Hw1_1.java as the name of the input file and Hw1_1.java contains:

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

// CLASS: Hw1_1 (Hw1_1.java)

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

public class Hw1_1 {

public static void main(String[] pArgs) {

}

public Hw1_1() {

}

}

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

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

[002] // CLASS: Hw1_1 (Hw1_1.java)

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

[004] public class Hw1_1 {

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

[006] }

[007] public Hw1_1() {

[008] }

[009] }

Hint: to print an integer as shown above in a field of width 3 with leading 0's use the printf() method with a format

specifier of %03d.

}

Explanation / Answer

import java.io.*;

import java.util.*;

public class Hw1_1

{

public static void main(String[] pArgs)

{

try{

// create a scanner type object

Scanner sc=new Scanner(System.in);

System.out.println("Enter the file name");

// read thee file name

String file_name = sc.nextLine();

BufferedReader inputStream = new BufferedReader(new FileReader(file_name+".java"));

// create a file object

File UIFile = new File(file_name+".java.txt");

  

// check if File exists

if (!UIFile.exists())

{

// create a new file

UIFile.createNewFile();

}

  

// create a FileWriter object

FileWriter filewriter = new FileWriter(UIFile.getAbsoluteFile());

  

// create a BufferedWriter object

BufferedWriter outputStream= new BufferedWriter(filewriter);

  

PrintWriter pw = new PrintWriter(System.out);

String count;

int temp = 0;

while ((count = inputStream.readLine()) != null){

// write the data to file

if(temp<10)

outputStream.write("[00" + temp + "]" + count + " ");

else if(temp<100)

outputStream.write("[0" + temp + "]" + count + " ");

else

outputStream.write("[" + temp + "]" + count + " ");

temp++;

}

outputStream.flush();

outputStream.close();

inputStream.close();

}

catch(IOException e)

{}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote