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

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set