Please write in JAVA ONLY Write a program that reads from th
Please write in JAVA ONLY
Write a program that reads from the inputFile.txt (provided as attachment to this assignment, it is down at the bottom in bold) and writes each line to a second file with the line numbers inserted. Example input and output.
inputFile.txt -
This is a test
outputFile.txt shoudl read
1. This is a test
-------Attachment is below-----
This is a test
to determine if you can
properly use exceptions
to manage the read and write from a
file.
Solution
LineText.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class LineText {
public static void main(String[] args) throws FileNotFoundException {
String fileName = \"inputFile.txt\";
String outputFile = \"outputFile.txt\";
File file = new File(fileName);
if(file.exists()){
Scanner scan1 = new Scanner(file);
PrintWriter pw = new PrintWriter(new File(outputFile));
int lineCount = 0;
String line;
while(scan1.hasNextLine()){
lineCount++;
line = scan1.nextLine();
pw.printf(\"%d. %s\ \", lineCount,line);
}
pw.flush();
pw.close();
System.out.println(\"Saving soruce code with line numbers to \"+outputFile);
}
else{
System.out.println(\"File does not exists\");
}
}
}
Output:
Saving soruce code with line numbers to D:\\outputFile.txt
Input file:
This is a test
to determine if you can
properly use exceptions
to manage the read and write from a
file.
Output file:
1. This is a test
2. to determine if you can
3. properly use exceptions
4. to manage the read and write from a
5. file.

