/ Published in: Java
Use this to read from a file. It is wrapped in a try/catch statement in case something breaks when trying to read the file. It will simply throw and exception (error) and continue rather than crashing your app.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import java.io.* /** * This program reads a text file line by line and prints them to the console. It uses * FileReader and BufferedReader to read the file. * */ public class FileInput { try { while (br.ready()) { // br.read() returns false when the Buffer is not ready (end of file). System.out.println(br.readLine()); // this statement reads a line from the file and prints it to the console (and moves to next line and is "ready" if there IS a next line). } br.close(); // dispose of the resources after using them. } e.printStackTrace(); // Put something here like an error message. } e.printStackTrace(); // Put something here like an error message. } } }