Using BufferedReader:
Output:
Hi guys !!
This is Ishaan from India.
and I'm reading the data from a text file line by line using BufferedReader.
Isn't it interesting !! to read the data from a file line by line
irrespective of amount of data that a file is having.
II Approach:
- import java.io.File;
- import java.io.FileReader;
- import java.io.BufferedReader;
- public class ReadFileByBufferedReader {
- public static void main(String[] args) {
- try {
- File file = new File("D://data.txt");
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- String line = br.readLine(); // read the line
- while (null != line) {
- System.out.println(line); // print the line
- line = br.readLine(); // read the next line
- }
- fr.close();
- } catch (Exception e) {
- System.out.println("excepton occured in " + e);
- }
- }
- }
Output:
Hi guys !!
This is Ishaan from India.
and I'm reading the data from a text file line by line using BufferedReader.
Isn't it interesting !! to read the data from a file line by line
irrespective of amount of data that a file is having.
II Approach:
- import java.io.File;
- import java.io.FileReader;
- import java.io.BufferedReader;
- public class ReadFileByBufferedReader {
- public static void main(String[] args) {
- File file = null;
- FileReader fr = null;
- BufferedReader br = null;
- try {
- file = new File("D://data.txt");
- fr = new FileReader(file);
- br = new BufferedReader(fr);
- String line = br.readLine(); // read the next line
- while (line != null) {
- System.out.println(line); // print the line
- line = br.readLine(); // read the next line
- }
- } catch (Exception e) {
- System.out.println("excepton occured in " + e);
- } finally {
- try {
- if (fr != null) {
- fr.close();
- }
- if (br != null) {
- br.close();
- }
- } catch (Exception e) {
- System.out.println("excepton occured in finally block " + e);
- }
- }
- }
- // support .java/.txt/.xml/.yml/.html/.css etc file
- }
No comments:
Post a Comment