Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:Java解决方案:如何正确使用UTF8StreamReader?发生错误的原因是:java.lang.ArrayIndexOutOfBoundsException:2048_Java_Byte_Javolution - Fatal编程技术网

Java:Java解决方案:如何正确使用UTF8StreamReader?发生错误的原因是:java.lang.ArrayIndexOutOfBoundsException:2048

Java:Java解决方案:如何正确使用UTF8StreamReader?发生错误的原因是:java.lang.ArrayIndexOutOfBoundsException:2048,java,byte,javolution,Java,Byte,Javolution,代码如下: public static void mergeAllFilesJavolution()throws FileNotFoundException, IOException { String fileDir = "C:\\TestData\\w12"; File dirSrc = new File(fileDir); File[] list = dirSrc.listFiles(); long start = System.currentTimeMill

代码如下:

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);
}
问题:如何在创建UTF8StreamReader时指定_字节的正确容量

尝试了File.length()但是它返回了long类型(我认为这是正确的,因为我期望文件的大小很大,但是构造函数只接收int类型)


任何关于正确方向的指导都是值得赞赏的。

似乎还没有人经历过上述情况

无论如何,我尝试了其他解决方案,没有使用上面的类(UTF8StreamReader),而是使用ByteBuffer(UTF8ByteBufferReader)。它比StreamReader快得难以置信

/**
 * 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();
}
/**
 * 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];
}