The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to read a file in java with example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
How to read a file in java with example program Posted: Aug 26, 2016 11:50 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: How to read a file in java with example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • We can read java file using buffered reader class in java
  • We need to import java.io.BufferedReader in order to read java text file.
  • Create object of java.io.BufferedReader class by passing new FileReader("C:\\Sample.txt") object to the constructor.
  • In order to read line by line call readLine() method of BufferedReader class which returns current line. Initially first line of java text file


 Program #1: Write a java program to read a file line by line using BufferedReader class

  1. package com.instanceofjava.javareadfile;

  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;

  5. public class ReadFileBufferedReader {

  6. /**
  7. * @Website: www.instanceofjava.com
  8. * @category: How to read java read file line by line using buffered reader
  9. */

  10. public static void main(String[] args) {

  11. BufferedReader breader = null;

  12.  try {

  13. String CurrentLine;

  14. breader = new BufferedReader(new FileReader("E://Sample.txt"));

  15. while ((CurrentLine = breader.readLine()) != null) {
  16. System.out.println(CurrentLine);
  17. }

  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. } finally {
  21.     try {
  22. if (breader != null)
  23. breader.close();
  24.      } catch (IOException ex) {
  25. ex.printStackTrace();
  26.      }
  27. }

  28. }

  29. }

 Output:
 
  1. Java open and read file
  2. Read file line by line in java
  3. Example java program to read a file line by line
  4. java read file line by line example
  
Program #2: Write a java program to read a file line by line using BufferedReader class using Eclipse

how to read a file in java

Read: How to read a file in java with example program

Topic: Binary tree InOrder traversal in Java using Recursion Previous Topic   Next Topic Topic: The Best Hadoop Analytics Solutions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use