Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reading Big File analysis

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Reading Big File analysis

    I was searching the prohram to read big file (i.e. size more than 4gb). I got the below program and it reads big files. They say that the program doesn't read the whole file at once. I would like to understand how its achieved in below program
    public class BigFile implements Iterable<String>
    {
    private BufferedReader _reader;

    public BigFile(String filePath) throws Exception
    {
    _reader = new BufferedReader(new FileReader(filePath));
    }

    public void Close()
    {
    try
    {
    _reader.close();
    }
    catch (Exception ex) {}
    }

    public Iterator<String> iterator()
    {
    return new FileIterator();
    }

    private class FileIterator implements Iterator<String>
    {
    private String _currentLine;

    public boolean hasNext()
    {
    try
    {
    _currentLine = _reader.readLine();
    }
    catch (Exception ex)
    {
    _currentLine = null;
    ex.printStackTrace();
    }

    return _currentLine != null;
    }

    public String next()
    {
    return _currentLine;
    }

    public void remove()
    {
    }
    }
    }

    Here’s how you might use it:
    BigFile file = new BigFile("C:\Temp\BigFile.txt");

    for (String line : file)
    System.out.println(line);


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading Big File analysis

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    the program doesn't read the whole file at once
    Unless the file has only one line, then the program reads the file one line at a time using the readLine() method.

Similar Threads

  1. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  2. Sales Analysis for various products
    By faridzul90 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 22nd, 2011, 08:36 AM
  3. Need help with reading from .dat file
    By cyoung in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 26th, 2011, 07:34 AM
  4. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  5. An online source offers project analysis
    By talha06 in forum The Cafe
    Replies: 0
    Last Post: June 20th, 2010, 12:49 PM