Java读取文件的前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");
}
你好,我希望有人能够帮助我解决我一直遇到的问题。我试图让我的程序读取指定文件的前 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是最简单的方法,但如果可用字节少于 10 个字节,则会抛出异常。如果您不想要它,请使用循环。不知何故,我不明白你在做什么,它看起来确实不像读取前 10 个字节:
if (byteCounter != 1000)
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)
问题在于
read
包含从流中有效读取的字节数。这些字节位于数组b
中。顺便说一句:你的数组大小为 8。如果你想读取 10 个字节,你应该将其增加到 10!
the problem is here
read
contains the number of bytes effectively read from stream. Teh bytes are in arrayb
.By the way: your array has a size of 8. you should increas it to 10 if you want to read 10 bytes!