用Java读取文件的前10个字节 [英] Reading the first 10 bytes of a file in Java

查看:1056
本文介绍了用Java读取文件的前10个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(字符串名称:文件名){
FileInputStream in = new FileInputStream(input.readUTF());

  
int byteCounter = 0;
int rowCounter = 0;
long bufferCounter = 0;
字节[] b =新字节[8];
int读取;

//in.skip(10); ((read = in.read())!= -1){
while((read = in.read(b,0,10))!= -1){
byteCounter ++;
if(byteCounter!= 1000){
if(rowCounter == 16){
System.out.println(\\\
);
rowCounter = 0;

System.out.print(Integer.toHexString(read)+\t);
bufferCounter ++;
rowCounter ++;
} else {
byteCounter = 0;
尝试{
Thread.sleep(200);
} catch(InterruptedException e){
}
}
}
System.out.println(\\\
+======= ====== + \\\
);

$ / code>

您好,我希望有人可以帮我解决一个问题我一直在。我试图让我的程序读取指定文件的前10个字节。我可以得到它与skip()的工作,但显然这与我想要的相反(它取代了前10)
我看了所有无济于事,如果你能帮助我,那会很棒。
您可能已经可以看到我已经尝试在代码中输入read(b,off,len),但是这只是产生随机字符而不是实际的十六进制字符74 65 71 etc编辑:这些随机字符似乎是读取的字节数的十六进制代码,所以对于有23个十六进制字符的文本文件,它产生一个3(或换句话说:10,10,3 = 23)

解决方案



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

read 包含从流中有效读取的字节数。 b
$ b

顺便说一下:你的数组的大小是8,如果你想读取10个字节,你应该把它增加到10!

    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)

解决方案

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!

这篇关于用Java读取文件的前10个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆