BufferedReaderとInputStreamReaderの処理速度

InputStreamReaderのJavaDocにあるBufferedReaderを使ったファイル読込を書いてみた。

    /**
     * BufferedReaderを使ったファイル読込.
     * @param filePath
     * @param charset
     * @return String
     * @throws IOException
     */
    public static String readFileb(String filePath, EncodeCharset charset) throws IOException{
        BufferedReader breader = null;
        try{
            breader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charset.getValue()));
            StringBuffer sb = new StringBuffer();
            int buf = -1;
            while((buf = breader.read()) != -1){
                sb.append((char)buf);
            }
            return sb.toString();
        }catch (IOException e) {
            LogUtil.error("ファイル読込に失敗", e);
            throw e;
        }finally{
            try{
                if(breader != null){
                    breader.close();
                }
            }catch (IOException e) {
                LogUtil.error("closeに失敗", e);
                throw e;
            }
        }
    }

すごく単純。 InputStreamReaderをBufferedReaderでラップするだけ。

いつか読んだ本にはこのへんのデザインパターンは理解すると凄く分かりやすくなると書かれていた。
中身忘れちゃったけど(笑)


そんで、どのくらい処理速度に違いがあるのか、実際に比較してみた。

今回はテストデータとしてなんちゃって個人情報を利用した。
# 落としたあとファイルをUTF8で保存し直した。

テスト用のTestCaseはこんな感じ。
読み込んでStringで持つまで。

    /**
     * ファイル読込テスト
     */
    public void testReadFile(){
        try{
            String inputPath = PropertyUtil.getPath(PropertySet.TestFileOutputPath);
            String fileName = "testInputFile.txt";

//            String data = "data";
            String filedata = FileUtil.readFile(inputPath+fileName, EncodeCharset.UTF_8);

//            assertEquals(data, filedata);
        }catch (Exception e) {
            LogUtil.error(e);
            fail();
        }
    }

    public void testReadFileb(){
        try{
            String inputPath = PropertyUtil.getPath(PropertySet.TestFileOutputPath);
            String fileName = "testInputFile.txt";

//            String data = "data";
            String filedata = FileUtil.readFileb(inputPath+fileName, EncodeCharset.UTF_8);

//            assertEquals(data.length(), filedata.length());
        }catch (Exception e) {
            LogUtil.error(e);
            fail();
        }
    }

結果はこちら!!!

ほほー。倍くらい違うんですね。

以上!