0

如果我通过阅读

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;
}
}
4

2 回答 2

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));
}
于 2013-10-13T20:22:52.493 回答
0

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

于 2013-10-13T20:38:49.383 回答