Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java Read File Line by Line - Java Tutorial

                         

In the section of Java Tutorial you will learn how to write java program to read file line by line. We will use the DataInputStream class to Read text File Line by Line.

Class DataInputStream
A data input stream is use to read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

Data input streams and data output streams represent Unicode strings in a format that is a slight modification of UTF-8. (For more information, see X/Open Company Ltd., "File System Safe UCS Transformation Format (FSS_UTF)", X/Open Preliminary Specification, Document Number: P316. This information also appears in ISO/IEC 10646, Annex P.) Note that in the following tables, the most significant bit appears in the far left-hand column.

BufferedReader

Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

 BufferedReader in
   = new BufferedReader(new FileReader("foo.in"));
 
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.

Here is the code of java program to Read text File Line by Line:

import java.io.*;
class FileRead 
{
   public static void main(String args[])
  {

      try{
    // Open the file that is the first 
    // command line parameter
    FileInputStream fstream = new FileInputStream("textfile.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

Download the code

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

15 comments so far (
post your own) View All Comments Latest 10 Comments:

how to get integer input values during runtime in java?

Posted by maheswari on Wednesday, 08.6.08 @ 11:08am | #71270

i want to 2 fields from one table and insert into second table but my tables are present on 2 different system one table in one system and another in second system i am using oracle but i want to develop program in Java plz help me

Posted by naina on Thursday, 03.6.08 @ 15:21pm | #51692

I tried using this, it works fine, but it seems to be discouraged because it is "deprecated"?

Posted by L-K on Monday, 03.3.08 @ 05:49am | #51072

Plz give me the source code that reading multiple files and write its contents to one file using FileFilter in Core Java. Plz help me.i need its urgent.

Posted by Naina on Thursday, 02.21.08 @ 15:56pm | #49310

Plz tell me how to read the contents of .doc file using java language

Posted by bala on Friday, 02.1.08 @ 09:16am | #46809

I'm Greatful to RoseIndia for providing with excellent study materials. Its very much user friendly, explaining things in a clear cut way
:) thanks a ton .......
-Anto

Posted by Anto on Thursday, 08.30.07 @ 18:40pm | #24520

Good but will it work in UTF UNICODE data file

Posted by Suresh on Monday, 08.27.07 @ 11:20am | #24234

THIS JAVA CODE IS HELPFUL FOR BEGINNERS..

Posted by ANURAG.R on Thursday, 08.9.07 @ 11:55am | #23001

ya its good but not the best upto what i wanted to be.coz this is quite complecated and each and every point should be clerified as we r the beginners.

Posted by sumit on Saturday, 06.16.07 @ 00:38am | #19347

This is wonderful tutorials not only for the freshers, even school/college teachers and professinals can read and share his knowledge.

Posted by satish singh on Wednesday, 03.14.07 @ 14:45pm | #11678

Latest Searches:
Java Read File Line by
HWPF
java conversion of dec
HttpSessionActivationL
displaying text in a d
resultsetmetadafta
core java Applets
how to write hql in da
C shell
align checkboxlist in
java mail with attachm
Visual Basic Date and
Java swing show()
mail merge in word
toInt()
multithrading
Take user input using
substring
Photoshop Web Layouts
logging
J2ME Gauge
ajax database conectiv
<select><option> jsp d
good desig
Sort the TreeMap by it
ASP EXAMPLE' and 1=1 a
Photoshop Photo Effect
updating two parameter
set colours Jtree
bean :write excel she
Tomcat deploy JSP
pb tree implementati
struts2 tag
session validation in
Boolean
Database MySQL Using S
struts project
how to write in pdf us
Hibernate Min() Functi
Combattons la programm
transientinjava
message beans
substring/com/gallery/
Eclipse Plugin- Editor
internationalized stru
sort array
Ajax with JSP
quiz
velocity rules engine
Create Scroll Bar in J
JButton with image
binary recursion
final keyword
Captcha
ajax struts
where
getting integer input
logout ajax struts2
How to write JSP
format a number decima
java jtable
java drop down list
icon a JButton
EJB 2.0 tutorials
Qt Jambi
count the number of di
J2ME Code Camp
session scope-manually
JSTL API
commons validator
program in java to ins
?ยà¸????ยà¸???ยà
MULTIPLE SELECT COMBO
check character
Photoshop Photo Effect
javascript separate in
jfreechart
Tutorial:FlashBasicPas
adding details in a js
how to retrieve values
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.