Create a Java NetBeans project named AddLineNumbers with the main class named Ad
ID: 3866396 • Letter: C
Question
Create a Java NetBeans project named AddLineNumbers with the main class named AddLineNumbers . This program will read data from a text file "dataInput.txt" and while there are more records, prepend a line number to the text data and output the concatenated data to "dataOutput.txt" implementing a try/catch block to catch any file errors thrown by the JVM. After you have created your NetBeans project AddLineNumbers and before you attempt to execute your application download and/or copy the text data file dataInput.txt from below to your AddLineNumbers project folder. * PLEASE FOLLOW THE INSTRUCTIONS AND STRUCTURE AS STRICTLY AS POSSIBLE. *
After you have created your NetBeans Project your application class should contain the following executable code: package addlinenumbers; public class AddLineNumbers f public static void main(String[I args)f Write the code for the main class as indicated below: package addlinenumbers; /* Import the Scanner class and all classes from java.io*/ // Your code here public class AddLineNumbers public static void main( String [ args ) /* Declare variable identifiers to handle text data input from the file and count/increment the linenumber * //Your code here /* Start try block */ // Your code hereExplanation / Answer
// For reading the File i have used the Scannar while for writing printWrite and FileOutputStream has been used. From input file each line has been read and appended with line number and written in to ouput File
package addlinenumbers;
import java.io.*;
import java.util.*;
public class AddLineNumbers {
public static void main(String[] args) {
try {
File file = new File("dataInput.txt");
Scanner input = new Scanner(file);
input = new Scanner(file);
PrintWriter pw = new PrintWriter( new FileOutputStream(new File("dataOutput")));
int lineNumber = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
++lineNumber;
line = lineNumber + " " + line +" ";
pw.print(line);
}
input.close();
pw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}