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);