快速读取文本文件逐个字符(java)

时间:2015-08-25 19:00:52

标签: java file

抱歉我的英文。我尝试逐字逐句快速读取大尺寸文本文件(不使用readLine())但尚未获得。我的代码:

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

它读取1GB文本文件56666ms,我怎样才能读得更快?

UDP

其方法读取 1GB 文件 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) {

        }

2 个答案:

答案 0 :(得分:2)

读取输入的最快方法是使用缓冲区。以下是具有内部缓冲区的类的示例。

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++];
   }
}

这个解析器有一个函数可以给你nextInt,如果你想要下一个char你可以调用read()函数。

这是从文件中读取的最快方式(据我所知)

您可以像这样初始化此解析器:

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

此代码在7782ms内读取250mb。

<强>声明: 代码不是我的,它已被用户'Kamalakannan CM'作为CodeChef问题的解决方案发布

答案 1 :(得分:1)

我会使用BufferedReader,它会读取缓冲区。简短的样本:

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

默认构造函数使用默认的buffersize为8192.如果要使用不同的缓冲区大小,可以使用this构造函数。或者,您可以读入数组缓冲区:

int char = reader.read();

或一次阅读一个字符:

private static Context m_context;

/*
call this function from Cocos2dxActivity.java 
AdsHelper.init(this);
*/
public static void initAds(Context context)
{
    m_context = context;
}

/*
call this from C++ from through JNI
*/
public static void showAds()
{
    ((Activity)m_context).runOnUiThread(new Thread(){
        @Override
        public void run() {

            super.run();
            Adservice adService = Adservice.getDefaultInstance();
            adService.showAds(m_context);
        }
     });

}