使用固定缓冲区大小将文件读取到byte []会生成重复输出

时间:2013-10-13 20:15:52

标签: java io

如果我通过

阅读
package net.example;

import java.io.FileInputStream;
import java.io.IOException;

public class Test {

public static void main(String[] args) throws IOException {
    byte[] buffer = new byte[1024];
    FileInputStream in = new FileInputStream("test.txt");
    int rc = in.read(buffer);
    while (rc != -1) {
        System.out.print(new String(buffer));
        rc = in.read(buffer);
    }
}
}

一个文本文件,它没有提供正确的内容。输出大于输入。

示例:http://pastebin.com/r5uGfYgD

我知道这是因为缓冲区大小。但是我怎么能告诉它在文件结束后停止阅读呢?

编辑:

现在它可以工作,这里是完整的来源。非常感谢!如果有人有一些改进:告诉我!

package net.example;

import java.io.FileInputStream;
import java.io.IOException;

import fr.cryptohash.Digest;
import fr.cryptohash.MD5;

public class Test {

public static void main(String[] args) throws IOException {
    Digest dig = new MD5();
    byte[] srcBuffer = new byte[102400];
    byte[] buffer = null;

    FileInputStream in = new FileInputStream("text.txt");

    int rc = -1;
    while ((rc = in.read(srcBuffer)) != -1) {
        buffer = new byte[rc];

        System.arraycopy(srcBuffer, 0, buffer, 0, rc);
        dig.update(buffer);
    }
    System.out.println(toHex(dig.digest()));
}

private static String toHex(byte[] hash) {
    char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    StringBuilder sb = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        sb.append(HEX_CHARS[(b & 0xF0) >> 4]);
        sb.append(HEX_CHARS[b & 0x0F]);
    }
    String hex = sb.toString();

    return hex;
}
}

2 个答案:

答案 0 :(得分:3)

如何使用String(bytes[], offset, length)构造函数?

byte[] buffer = new byte[1024];
FileInputStream in = new FileInputStream("input.txt");
int rc = -1;
while ((rc = in.read(buffer)) != -1) {
    System.out.print(new String(buffer, 0, rc));
}

答案 1 :(得分:0)

如果需要将文件内容读取到byte [],则可以使用ByteArrayOutputStream或使用具有“read to byte []”util方法的commons io。