g8214435
g8214435

Reputation: 737

Fast read text file character-by-character(java)

Sorry for my english. I try read realy fast big size text file character-by-character(not use readLine()) but it has not yet obtained. My code:

for(int i = 0; (i = textReader.read()) != -1; ) {
            char character = (char) i;
        }

It read 1GB text file 56666ms, how can i read faster?

UDP

Its method read 1GB file 28833ms

FileInputStream fIn = null;
        FileChannel fChan = null;
        ByteBuffer mBuf;
        int count;

        try {
            fIn = new FileInputStream(textReader);
            fChan = fIn.getChannel();
            mBuf = ByteBuffer.allocate(128);

            do {
                count = fChan.read(mBuf);

                if(count != -1) {
                    mBuf.rewind();

                    for(int i = 0; i < count; i++) {
                        char c = (char)mBuf.get();
                    }
                }

            } while(count != -1);

        }catch(Exception e) {

        }

Upvotes: 2

Views: 1340

Answers (2)

Olimpiu POP
Olimpiu POP

Reputation: 5067

I would use BufferedReader, it reads buffered. A short sample:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.CharBuffer;

public class Main {
    public static void main(String... args) {
        try (FileReader fr = new FileReader("a.txt")) {
            try (BufferedReader reader = new BufferedReader(fr)) {
                 CharBuffer charBuffer = CharBuffer.allocate(8192);
                 reader.read(charBuffer);
            } catch (IOException e) {
                 e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
           e.printStackTrace();
        } catch (IOException e) {
           e.printStackTrace();
        }
   }
}

The default constructor uses a default buffersize of 8192. In case you want to use a different buffer size you can use this constructor. Alternatively you can read in an array buffer:

 ....
 char[] buffer = new char[255];
 reader.read(buffer);
 ....

or read one character at a time:

int char = reader.read();

Upvotes: 1

Tawcharowsky
Tawcharowsky

Reputation: 625

The fastest way to read input is to use buffer. Here is an example of a class that has internal buffer.

class Parser
{
   final private int BUFFER_SIZE = 1 << 16;
   private DataInputStream din;
   private byte[] buffer;
   private int bufferPointer, bytesRead;

   public Parser(InputStream in)
   {
      din = new DataInputStream(in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
   }

   public int nextInt() throws Exception
   {
      int ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      //boolean neg = c == '-';
      //if (neg) c = read();
      do
      {
         ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      //if (neg) return -ret;
      return ret;
   }

   private void fillBuffer() throws Exception
   {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1) buffer[0] = -1;
   }

   private byte read() throws Exception
   {
      if (bufferPointer == bytesRead) fillBuffer();
      return buffer[bufferPointer++];
   }
}

This parser has function that will give you nextInt, if you want next char you can can call read() function.

This is the fastest way to read from a file (as far as I know)

You would initialize this parser like this:

Parser p = new Parser(new FileInputStream("text.txt"));
int c;
while((c = p.read()) != -1)
    System.out.print((char)c);

This code reads 250mb in 7782ms.

Disclaimer: the code is not mine, it has been posted as a solution to a problem on CodeChef by the user 'Kamalakannan CM'

Upvotes: 2

Related Questions