老师我有个问题请教一下,我的代码: 可以直接看加粗字体部分
/** 实现批量读取文件 参数为文件路径
* @param dir
* @throws IOException
*/
public static void readByBytes(String dir) throws IOException
{
FileInputStream in= new FileInputStream(dir);
byte[] temp=new byte[1024*5];
int count=0;
int j=1;
while((count=(in.read(temp, 0, temp.length)))!=-1)
{
for(int i=0;i<count;i++)
{
if(temp[i]<=0xf)
System.out.print("0");
System.out.print(Integer.toHexString(temp[i]&0xff)+" ");
if(j++%10==0)
System.out.println();
}
}
in.close();
}
问题:加粗那段是判断如果小于八位,就添加一个0在前面,可是输出的结果有点不太一样,
例如
0d8 0f6 0a1 7b 03 0f9 0d0 30 5c 094
24 08e 47 0af 7a 63 36 0e 79 07
为什么有的输出是三位,可是最神奇的是,基本上同样的代码,只不过在逐个字节读取里,输出就是正常的。
逐个字节读取代码如下:
/**
* 实现逐个字节读取文件 参数为文件路径
* @param dir
* @throws IOException
*/
public static void readByByte(String dir) throws IOException
{
FileInputStream in=new FileInputStream(dir);
int temp=0;
int j=1;
while((temp=in.read())!=-1)
{
if(temp<=0xf)
System.out.print("0");
System.out.print(Integer.toHexString(temp&0xff)+" ");
if(j++%10==0)
System.out.println();
}
in.close();
}
输出结果一切正常:
4d 08 73 0c 54 2e bf 29 35 43
25 81 b2 98 a5 1d 4d 08 4c 4e
77 53 94 1c d5 0d 12 8e 94 2f
5a 42 63 24 05 4e 7b 50 ad eb
40 27 72 5e 94 f5 8b 78 a1 8c
求解 很纳闷