Read a Text File in JAVA

No.of Views620
Bookmarked0 times
Downloads 
Votes0
By  youngmurukan   On  15 Feb 2010 21:02:15
Tag : JAVA , How to
Read a Text File in JAVA
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article has detail to read a text file in java with simple java IO class.

Sample text File

Image Loading....

if you want to read this file content, first you have to import the java.io.* class for input/output operations.

Then create objects from these 4 classes, which are
1.File - To access the Text file which want to read.
2.FileInputStream – To Read the file content.
3.BufferedInputStream – To buffer the read streams
4.DataInputStream – To read the data whatever in buffer.

Or else you can import these classes individually as shown below,

Image Loading....

First method would be preferable as it is reducing your Line of codes (LOC).


Here it is the whole source code for this,

Image loading...

Explanation:


Foremost we need to create File object and give the path of the Text file (which you want to read) as constructor.


Read the file content as stream by using FileInputStream and put it into Buffer as passing the reference of FileInputStream to constructor parameter of BufferedInputStream .
And read this buffer content by using DataInputStream.

Output of above source,

Image Loading....

Whatever in Text File it would give as result.

Download Source Code


Thank you for reading.

Murukan

 
Sign Up to vote for this article
 
About Author
 
youngmurukan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 May 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top