Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  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.

 

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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

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

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

Thank You!!! I REALLY needed this

Posted by RK on Tuesday, 04.7.09 @ 20:58pm | #86619

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

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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 | Flex 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.