InputStream read(byte[] b, int off, int len) - 删除文件的其余部分?

发布于 2024-10-13 16:07:42 字数 1425 浏览 2 评论 0原文

这是我今天早上在这里提出的一个问题的扩展,所以如果您认为您以前看过此代码,请不要忽视:D

for (String name : filenames) {
FileInputStream in = new FileInputStream(input.readUTF());
    int byteCounter = 0;
    int rowCounter = 0;
    long bufferCounter = 0;
    byte[] b = new byte[10];
    int read;

    //in.skip(10);
    //while((read = in.read()) != -1){
    while((read = in.read(b, 0, 10)) != -1){
        byteCounter ++;
        if (byteCounter != 1000){
            if (rowCounter == 1){
                System.out.println("\n");
                rowCounter = 0;
            }
        System.out.print(org.apache.commons.codec.binary.Hex.encodeHexString(b));
            bufferCounter ++;
            rowCounter ++;
        }else{
                byteCounter = 0;
                try{
                    Thread.sleep(200);
                }catch(InterruptedException e) {
                }
        }
    }
    System.out.println("\n"+"================"+"\n");
}

您好,经过几个小时的努力才得到此代码怎么样,我几乎已经完成了我正在处理的特定组件。该程序接受指定的文件,并应该将该文件的前 10 个字节转换为十六进制。一旦它获取了该文件的前 10 个字节,它应该停止并移动到下一个指定的文件。目前,它会将整个文件分成多个 10 字节的“块”,然后将其打印出来。换句话说,它在我认为 read(byte[] b, int off, int len) 执行的前 10 个字节之后并没有停止

示例输出:

74656173676173677361

67616773617367616773

61676173617367616773

但它应该产生

74656173676173677361

任何建议将不胜感激,我真的是这个意思:)

This is an extension of a question I asked on here this morning so please don't disregard if you think you've seen this code before :D

for (String name : filenames) {
FileInputStream in = new FileInputStream(input.readUTF());
    int byteCounter = 0;
    int rowCounter = 0;
    long bufferCounter = 0;
    byte[] b = new byte[10];
    int read;

    //in.skip(10);
    //while((read = in.read()) != -1){
    while((read = in.read(b, 0, 10)) != -1){
        byteCounter ++;
        if (byteCounter != 1000){
            if (rowCounter == 1){
                System.out.println("\n");
                rowCounter = 0;
            }
        System.out.print(org.apache.commons.codec.binary.Hex.encodeHexString(b));
            bufferCounter ++;
            rowCounter ++;
        }else{
                byteCounter = 0;
                try{
                    Thread.sleep(200);
                }catch(InterruptedException e) {
                }
        }
    }
    System.out.println("\n"+"================"+"\n");
}

Hi there, after hours of struggling to get this code to how it is, I've almost finished the particular component that I am working on. The program takes in a specified file and is supposed to convert the first 10 bytes of that file into Hex. Once it has acquired the first 10 bytes of that file, it should stop and move onto the next specified file. Currently, it takes the entirety of the file and divides it into multiple 10 byte 'chunks' which it then prints out. In other words, it isn't stopping after the first 10 bytes which I thought read(byte[] b, int off, int len) did

Example output:

74656173676173677361

67616773617367616773

61676173617367616773

but instead it should produce

74656173676173677361

Any advice would be hugely appreciated it and I really do mean that :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-10-20 16:07:42

放弃 while 循环怎么样?正如我在你的第一个中已经告诉你的问题,如果您确定总是至少有 10 个字节,则可以采用以下方法:

byte[] b = new byte[10];
new DataInputStream(new FileInputStream(input.readUTF())).readFully(b);

否则请执行以下操作:

byte[] b = new byte[10];
int count = 0;
while (count < b.length) {
    int n = in.read(b, count, b.length-count);
    if (n==-1) break;
    count += n;
}

What about dropping the while-loop? As I already told you in your first question, the following is the way to go in case you're sure there are always at least 10 bytes:

byte[] b = new byte[10];
new DataInputStream(new FileInputStream(input.readUTF())).readFully(b);

Otherwise go for something like this:

byte[] b = new byte[10];
int count = 0;
while (count < b.length) {
    int n = in.read(b, count, b.length-count);
    if (n==-1) break;
    count += n;
}
酒几许 2024-10-20 16:07:42

外部 for 循环 ( while((read = in.read(b, 0, 10)) != -1) ) 中有一个 while 循环,它循环遍历整个文件中的所有字节。

尝试一个简单的 for(int i = 0; i < 10; i++) 从 in 读取单个字节,并将其转换为十六进制。

并且不要忘记在 finally{} 子句中关闭文件和输入流!

You have a while loop inside your outer for loop ( while((read = in.read(b, 0, 10)) != -1) ) that is cycling through all of the bytes in the whole file.

Try a simple for(int i = 0; i < 10; i++) that reads a single byte from in, and converts those to hex.

And don't forget to close your files and input streams in a finally{} clause!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文