write a program called MyTail in java that takes a filetxt a
write a program called MyTail in java that takes a file.txt and will print out the last N elements of that file. include Stdln.readLine(). use a data structure of your choice. (in java)
Solution
package com.chegg.file;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class MyTail {
public static void main(String[] args) throws IOException {
try {
FileInputStream input = new FileInputStream(\"file.txt\");
// Read file called file.txt
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String lastLine = null;
String temp;
// reading file line by line up null condition and store value to temp variable
while((temp = reader.readLine()) != null){
lastLine = temp;
}
// store last line content into lastLine varibale
System.out.println(\"LastLine :\"+lastLine);
// close file
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
