Any help with the code for this, thanks in advance 4.3 Name your source code fil
ID: 3916108 • Letter: A
Question
Any help with the code for this, thanks in advance
4.3 Name your source code file Hw1_I.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 HwI_1java as the name of the input file and Hwl_1java contains 7CLASS: Hwl1 (Hw11.java) public class Hw1 1 public static void main (String[] pArgs) public Hw1 1 then the contents of the output file Hwl_1java.txt would be: 001 //* 002] 7/ CLASS: Hw11 (Hw1 1.java) 003 //* 004] public class Hwl 1 ( 005] (006] 007] 008] [009] ) public static void main (String[) pArgs)( public Hw1 10 method with a format Hint: to print an integer as shown above in a field of width 3 with leading O's use the printf specifier of %03d.Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Converter {
public static void main(String args[]) throws IOException {
// FileName Read
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter complete file name with path : ");
String fileName = reader.readLine();
// File read
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
// Output to a file with append mode false
FileWriter writer = new FileWriter(fileName + ".txt", false);
String st;
int count = 1;
while ((st = br.readLine()) != null) {
writer.write(String.format("[%03d] %s ", count, st));
count++;
}
writer.close();
// File write complete
System.out.println("File write complete to : " + fileName);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.