-1

可能重复:
任何提高读取文件性能的方法,比缓冲读取器更好

我正在通过缓冲阅读器读取一个名为 ty.log 的文件,该文件大小为 20 mb,请告知如何通过 NIO 读取相同的内容。下面是我的程序,请告诉我如何转换,以便我可以通过 java nio 读取相同的文件也在jdk 1.6下

public static void main(String[] args)
{

    BufferedReader br = null;
    long startTime = System.currentTimeMillis();

    try
    {
        String sCurrentLine;

        br = new BufferedReader(new FileReader("C://ty.log"));

        while ((sCurrentLine = br.readLine()) != null)
        {

        }
        long elapsedTime = System.currentTimeMillis() - startTime;

        System.out.println("Total execution time taken in millis: " + elapsedTime);

    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            if (br != null)
                br.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
}
4

1 回答 1

0

对于使用 nio 读取文件,您可以使用java.nio.channels.FileChannel

班级。 Javadoc解释了这一切。

于 2012-09-19T17:06:42.450 回答