0

コードは次のとおりです。

public static void mergeAllFilesJavolution()throws FileNotFoundException, IOException {
    String fileDir = "C:\\TestData\\w12";
    File dirSrc = new File(fileDir);
    File[] list = dirSrc.listFiles();
    long start = System.currentTimeMillis();
    for(int j=0; j<list.length; j++){
        int chr;
        String srcFile = list[j].getPath();
        String outFile = fileDir + "\\..\\merged.txt";
        UTF8StreamReader inFile=new UTF8StreamReader().setInput(new FileInputStream(srcFile));
        UTF8StreamWriter outPut=new UTF8StreamWriter().setOutput(new FileOutputStream(outFile, true)); 
        while((chr=inFile.read()) != -1) {
            outPut.write(chr);
        }
        outPut.close();
        inFile.close();
    }
    System.out.println(System.currentTimeMillis()-start);
}

utf-8ファイルのファイルサイズはテストデータとして200MBですが、800MBアップの可能性が高いです。

UTF8StreamReader.read() のソース コードは次のとおりです。

/**
 * Holds the bytes buffer.
 */
private final byte[] _bytes;

/**
 * Creates a UTF-8 reader having a byte buffer of moderate capacity (2048).
 */
public UTF8StreamReader() {
    _bytes = new byte[2048];
}

/**
 * Reads a single character.  This method will block until a character is
 * available, an I/O error occurs or the end of the stream is reached.
 *
 * @return the 31-bits Unicode of the character read, or -1 if the end of
 *         the stream has been reached.
 * @throws IOException if an I/O error occurs.
 */
public int read() throws IOException {
    byte b = _bytes[_start];
    return ((b >= 0) && (_start++ < _end)) ? b : read2();
}

_bytes = new byte[2048] であるため、_bytes[_start] でエラーが発生します。

別の UTF8StreamReader コンストラクターを次に示します。

/**
 * Creates a UTF-8 reader having a byte buffer of specified capacity.
 * 
 * @param capacity the capacity of the byte buffer.
 */
public UTF8StreamReader(int capacity) {
    _bytes = new byte[capacity];
}

問題: UTF8StreamReader の作成時に _bytesの正しい容量を指定するにはどうすればよいですか?

File.length()を試しましたが、長い型が返されます (巨大なファイルサイズが予想されますが、コンストラクターは int 型のみを受け取るため、正しいと思います)。

正しい方向へのガイダンスをいただければ幸いです。

4

1 に答える 1

0

上記の状況で同じことを経験した人はまだいないようです。

とにかく、上記のクラス(UTF8StreamReader)ではなくByteBuffer(UTF8ByteBufferReader)を使用しないことで、他の解決策を試しました。StreamReader よりも信じられないほど高速です。

ByteBuffer を使用したファイルのマージの高速化

于 2011-06-30T01:05:54.237 に答える