Write a java program that reads from the inputFiletxt provid

Write a java program that reads from the inputFile.txt (provided as attachment to this assignment) 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

The outputFile.txt is a note pad that says:

This is a test
to determine if you can
properly use exceptions
to manage the read and write from a
file.

Solution

inputFile.txt ( Save this file under D Drive.Then the path of the file pointing to that file is D:\\\\inputFile.txt)

This is a test

__________________

FileReadWrite.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReadWrite {

   public static void main(String[] args) {
       String line=null;
       try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(\"D:\\\\inputFile.txt\");

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
  
   //Creating FileWriter object by passing output file name as input.
   FileWriter fw=new FileWriter(\"D:\\\\outputFile.txt\");
   BufferedWriter bw=new BufferedWriter(fw,1024);
   bw.write(line);
   bw.close();
   fw.close();
}

//closing buffer reader
bufferedReader.close();   
}
       catch(FileNotFoundException ex) {
System.out.println(\"Could\'nt able to open file \");
}
       catch(IOException ex) {
   System.out.println(\"Error reading file \");
     
   }
   }

}

__________________________________________

Output:

outputFile.txt (We will get this file in D Drive as we specified the path as D:\\\\outputFile.txt)

This is a test

_______________________Thank You

Write a java program that reads from the inputFile.txt (provided as attachment to this assignment) and writes each line to a second file with the line numbers i
Write a java program that reads from the inputFile.txt (provided as attachment to this assignment) and writes each line to a second file with the line numbers i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site