package com.imooc;
import java.io.FileInputStream;
import java.io.IOException;
public class IOUtil {
/*
*
*/
public static void printHex(String fileName)throws IOException{
FileInputStream in=new FileInputStream(fileName);
int b=in.read();
int i=1;
while(b!=-1){
if(b<=0xf){
System.out.println();
}
System.out.print(Integer.toHexString(b)+" ");
if(i++%10==0){
System.out.println();
}
}
in.close();
}
public class Text {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
IOUtil.printHex("C:\\Users\\dell\\Desktop\\io.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
你好,你的 printHex()函数写的有问题,你一开始只读了一个字节,while循环中b一直是68,不会等于-1,所以一直输出68,在while循环最后加一句b=in.read();就可以了
public static void printHex(String fileName)throws IOException{ FileInputStream in=new FileInputStream(fileName); int b=in.read(); int i=1; while(b!=-1){ if(b<=0xf){ System.out.println(); } System.out.print(Integer.toHexString(b)+" "); if(i++%10==0){ System.out.println(); } b=in.read(); } in.close(); }
while((b=in.read())!=-1)的意思是循环读取一个字节,直到所读取的字节数为-1时,就会结束读取操作!
while条件写错了。。b=in.read()-1