Java读取文件的前10个字节

发布于 2024-10-14 04:03:56 字数 1260 浏览 1 评论 0原文

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

    //in.skip(10);   
    //while((read = in.read()) != -1){   
    while((read = in.read(b, 0, 10)) != -1){   
        byteCounter ++;   
        if (byteCounter != 1000){   
            if (rowCounter == 16){   
                System.out.println("\n");   
                rowCounter = 0;   
            }   
        System.out.print(Integer.toHexString(read) + "\t");   
            bufferCounter ++;   
            rowCounter ++;   
        }else{   
                byteCounter = 0;   
                try{   
                    Thread.sleep(200);   
                }catch(InterruptedException e) {   
                }   
        }   
    }   
    System.out.println("\n"+"================"+"\n");   
}  

你好,我希望有人能够帮助我解决我一直遇到的问题。我试图让我的程序读取指定文件的前 10 个字节。我可以让它与skip()一起工作,但显然这与我想要的相反(它删除了前10个) 我查遍了也没有用,如果你能帮我的话,那就太好了。 您可能会看到我已经尝试将 read(b, off, len) 输入到代码中,但这只会产生随机字符作为输出,而不是我想要的实际十六进制字符 74 65 71 等(编辑:这些随机字符字符似乎是读取字节数的十六进制代码,因此对于其中包含 23 个十六进制字符的文本文件,它会生成 aa 3 (或者换句话说:10,10,3 = 23)。

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

    //in.skip(10);   
    //while((read = in.read()) != -1){   
    while((read = in.read(b, 0, 10)) != -1){   
        byteCounter ++;   
        if (byteCounter != 1000){   
            if (rowCounter == 16){   
                System.out.println("\n");   
                rowCounter = 0;   
            }   
        System.out.print(Integer.toHexString(read) + "\t");   
            bufferCounter ++;   
            rowCounter ++;   
        }else{   
                byteCounter = 0;   
                try{   
                    Thread.sleep(200);   
                }catch(InterruptedException e) {   
                }   
        }   
    }   
    System.out.println("\n"+"================"+"\n");   
}  

Hi there, I was hoping someone might be able to help me with an issue I've been having. I'm trying to get my program to read in the first 10 bytes of a specified file. I can get it to work with the skip() but obviously this does the opposite of what I want (it removes the first 10 instead)
I've looked all over to no avail, if you can help me out, that'd be great.
You can probably see that I've tried to enter the read(b, off, len) into the code already but this just produces random characters as an output rather than the actual hex characters I want 74 65 71 etc (Edit: These random characters seem to be the hex code for the number of bytes read. So for a text file that has 23 hex chars in it, it produces a a 3 (or in other words: 10,10,3 = 23)

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

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

发布评论

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

评论(2

浅沫记忆 2024-10-21 04:03:56
byte[] b = new byte[10];
new DataInputStream(new FileInputStream(input.readUTF())).readFully(b);

这是最简单的方法,但如果可用字节少于 10 个字节,则会抛出异常。如果您不想要它,请使用循环。不知何故,我不明白你在做什么,它看起来确实不像读取前 10 个字节: if (byteCounter != 1000)

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

This is the simplest way, but it throws in case there are less than 10 bytes available. In case you don't want it, use a loop. Somehow I don't get what you're doing, it really doesn't look like reading the first 10 bytes: if (byteCounter != 1000)

孤千羽 2024-10-21 04:03:56

问题在于

System.out.print(Integer.toHexString(read) + "\t");

read 包含从流中有效读取的字节数。这些字节位于数组 b 中。

顺便说一句:你的数组大小为 8。如果你想读取 10 个字节,你应该将其增加到 10!

the problem is here

System.out.print(Integer.toHexString(read) + "\t");

read contains the number of bytes effectively read from stream. Teh bytes are in array b.

By the way: your array has a size of 8. you should increas it to 10 if you want to read 10 bytes!

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