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
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.