Java缓冲区的大小 [英] Java―good size for a char buffer

查看:677
本文介绍了Java缓冲区的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于java的小工具来处理mysqldump文件,这些文件可能变得非常大(目前高达一千兆字节)。我正在使用此代码来读取和处理文件:

I am coding a little java based tool to process mysqldump files, which can become quite large (up to a gigabyte for now). I am using this code to read and process the file:

BufferedReader reader = getReader();
BufferedWriter writer = getWriter();

char[] charBuffer = new char[CHAR_BUFFER_SIZE];
int readCharCout;
StringBuffer buffer = new StringBuffer();

while( ( readCharCout = reader.read( charBuffer ) ) > 0 )
{
    buffer.append( charBuffer, 0, readCharCout );
    //processing goes here
}

什么是好的尺寸 CharBuffer的?目前设置为1000,但我的代码将以任意大小运行,那么最佳做法是什么,或者根据文件大小计算这个大小?

What is a good size for the charBuffer? At the moment it is set to 1000, but my code will run with an arbitrary size, so what is best practice or can this size be calculated depending on the file size?

在此先感谢,
greetings philipp

Thanks in ahead, greetings philipp

推荐答案

它应始终为2的幂。最佳值基于在操作系统和磁盘格式上。在代码中,我经常使用4096,但越大越好。

It should always be a power of 2. The optimal value is based on the OS and disk format. In code I've seen 4096 is often used, but the bigger the better.

此外,还有更好的方法将文件加载到内存中。

Also, there are better ways to load a file into memory.

这篇关于Java缓冲区的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆