问答详情
源自:4-1 字节流之文件输入流FileInputStream-1

单字节的问题

不明白为何添加了System.out.print(0);这句后就可以在单字符前面加0?

提问者:慕粉3170877 2016-07-09 16:08

个回答

  • lsnFor
    2016-07-09 18:29:26
    已采纳

    /**
    	 * 用十六进制打印指定文件 每隔十个字节换行
    	 * 单字节读取
    	 * @param fileName
    	 * @throws IOException
    	 */
    	public static void printHex(String fileName) throws IOException {
    		FileInputStream in = new FileInputStream(fileName);
    		int b;
    		int i = 1;
    		while ((b = in.read()) != -1) {
    			if (b <= 0xf) {
    				/*
    				 * 将读到的每个字节对象和0xf进行比较--->0x表示十六进制 f表示15
    				 * 如果字节对象小于等于15(f),在打印之前先打印一个“0”
    				 */
    				System.out.print("0");
    			}
    			
    			System.out.print(Integer.toHexString(b) + " ");
    			
    			if (i++ % 10 == 0) {
    				//i先+1,每打印十个字节,进行打印换行
    				System.out.println();
    			}
    		}
    		in.close();
    	}