In: Computer Science
Write the programs in JavaScript:
Write a program that reads a text file and outputs the text file
with line numbers at the beginning of each line.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class WriteLineNumbers {
public static void main(String[] args){
try {
String
line;
int i = 1;
BufferedWriter
bw = null;
FileWriter fw =
null;
Scanner sc = new
Scanner(System.in);
System.out.println("Enter filename: ");
String
fname=sc.next();
sc = new
Scanner(new File(fname));
fw = new
FileWriter("output.txt");
bw = new
BufferedWriter(fw);
while
(sc.hasNext()) {
line = sc.nextLine();
bw.write(i + " " + line + "!\n");
i++;
}
bw.close();
sc.close();
}catch(Exception e)
{e.printStackTrace();}
System.out.println("Written
Successfully....!!!");
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me