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

help here is the file Write a program that open the MyFile1.txt or MyFileO.trt,

ID: 3911205 • Letter: H

Question

help
here is the file

Write a program that open the MyFile1.txt or MyFileO.trt, reads the lines of the file and display to your output with these criteria. 1)-The program should display the first five lines of the files contents only. You Must check for end of the file on your loop 2)If the file contains less than five lines, it should display the file's entire content. You must check for end of file and less than 6 lines on the loop iterations. 3)- your program should use loop to read the line by line of the file MyFile1.txt. Hint: Read your book pares 214-221; save your program File Reader java and the MyFile1.txt in same d irectory. Do not change the content of MyFile.txt. Here is the image sample GFile: FilReader java Fl Gig 512ME Hcer Classestcosc 1436 JanvalExams Exam2ExamCode- jGRASP CSD Ua. litile Edit yew Build Project Settings lools?now telp FIReader java RMGig512MBHccs Classes COSC 1436 Jarva Exams Exarn2/ExamCod... . mport Java.i0. pub 1 Lc cla33 File Reader publio static void main(String(] ags) throvs TOExcept son int counter-0: String str: FileReader treader nev FileReader ("MyFile.EXt") Browse Find Nagd/ ??? Debug WorkbenchFIReade... MyFile0 Tat Decument MFil Tert Decument Compile Messages jGRASP Messages Run LC End I can st 111 remenber, at the end of that year, ve sat for the last t She played us her tavorite song, "like a bridge over trobled vater" and quietly passed out a gitt she had nade for cach of us She had madr each of us a pottery heart vith our name aad the date "T believe in ME" .Ve all cried inciuding her. Clear Help GRASP: operation compiete

Explanation / Answer

Code for the following problem in Java can be like this :

import java.io.*;

public class File_Reader

{

public static void main(String [] args) throws IOException

{

String fileName = "MyFile0.txt"; // This is used to store the name of file that you want to read, it can be MyFile0.txt or MyFile1.txt

String line = null; // Variable used to store the line read from file

BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName)); // File reader function

int i = 0; // Control variable used to control the number of lines that we want to read in this case 5

try {

//print first 5 lines or all if file has less than 5 lines, first condition checks whether end of file is reached or not in case lines are less than 5, this will terminate the loop else the second condition is used which prints initial 5 lines

while(((line = bufferedReader.readLine()) != null) && i < 5) {

System.out.println(line); // print line

i++; // increment i as subsequent line is read

} // end of while loop

} // End of try block

finally { bufferedReader.close(); } // close the file reader

} // end of main

} // end of file reader class

*This code will gives the required result as per your files.

Thankyou

Please Upvote.