Write a Java program to read first 3 lines from the file
import java.io.*;
class App {
public static void main(String[] args) {
String fileName = "text.txt";
String line = null;
int i = 0;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
while (((line = bufferedReader.readLine()) != null) && i < 3) {
System.out.println(line);
i++;
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Comments
Leave a comment