How to read a file in Java
In Java, you can use the BufferedReader class as follows in order to read a file:
package demo;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class MyTextFileReader {public static void main(String args[]){try {BufferedReader br = new BufferedReader(new FileReader(new File("F:/test.txt")));String line = null;while ((line = br.readLine()) != null) {System.out.println(line);}} catch ( IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
Comments
Post a Comment