Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class name FilcDisplay with the following methods: Constructor: takes th

ID: 3698314 • Letter: W

Question

Write a class name FilcDisplay with the following methods: Constructor: takes the name of a flic as an argument displayHcad: displays only the first five lines of the file's contents. If the file contains less than 5 lines, it should display the file's entire contents. displayContcnts: displays the entire contents of the file, the name of which was passed to the constructor. writcNcw: displays the contents of a file, the name of which was passed to the constructor. Each line should be preceded with a line number followed by a colon. The line numbering should start at 1. As cach line is displayed it should also be written to a new file, the name of which is passed in as a parameter. I have provided a test file callcd "tcstfilc.txt". I have also provided a driver for the application callcd "filcdisplaytcst.java". This file is complete and should not be altered.

Explanation / Answer

//please make sure all the files should be at one position

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public final class FileDisplay {

public FileDisplay(String p) throws IOException{
String x;
x=p;
displayContents(x);
displayHead(x);
WriteNew(x);
}
   public static void main(String[] args) throws IOException {

       //here in the constructor file name is send as a parameter
FileDisplay fil=new FileDisplay("testFile.txt");
  
   }
//display the contents of a file
public void displayContents(String x) throws FileNotFoundException, IOException{
BufferedReader in = new BufferedReader(new FileReader(x));
String line;
while((line = in.readLine()) != null)
{
  
System.out.println(line);
}
in.close();

}
//this method is used to display line numbers followed by colon
public void WriteNew(String x) throws FileNotFoundException, IOException{

int linenumber=1;
BufferedReader in = new BufferedReader(new FileReader(x));
String line;
while((line = in.readLine()) != null)
{
  
System.out.println(linenumber+ ":"+line);
linenumber++;


}
in.close();
copyFile(x,"file2.txt");
}
//copy the contents of one text file into another file
public void copyFile(String x,String y)
{
/* Source file, from which content will be copied */
       File sourceFile = new File(x);

       /* destination file, where the content to be pasted */
       File destFile = new File(y);
      
       /* if file not exist then create one */
       if (!destFile.exists()) {
           try {
               destFile.createNewFile();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

       InputStream input = null;
       OutputStream output = null;

       try {

           /* FileInputStream to read streams */
           input = new FileInputStream(sourceFile);

           /* FileOutputStream to write streams */
           output = new FileOutputStream(destFile);

           byte[] buf = new byte[1024];
           int bytesRead;

           while ((bytesRead = input.read(buf)) > 0) {
               output.write(buf, 0, bytesRead);
           }

       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }

       finally {
           try {

               if (null != input) {
                   input.close();
               }
              
               if (null != output) {
                   output.close();
               }

           } catch (IOException e) {
           }
       }
}
//display the contents of a file upto 5 lines
public void displayHead(String x) throws FileNotFoundException, IOException{
BufferedReader in = new BufferedReader(new FileReader(x));
String line;
int flag=1;
while((line = in.readLine()) != null)
{
if(flag<=5)
{
System.out.println(line);
flag++;
}
}
in.close();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote