Java - Read a file and display the contents line by line
Java - Read a file and display the contents line by line
CODE
import java.io.*;
public class Main
{
public static void main(String[] args) {
try {
String fileName = "input.txt";
File f = new File(fileName);
if(f.exists()) {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine())!= null) {
System.out.println(line);
}
fr.close();
}
else {
System.out.println("The file does not exist.");
}
//OUTPUT
//Displays the contents of the file line by line
}
catch(IOException e) {
System.out.println(e.toString());
}
}
}
Comments
Post a Comment