单字节的问题

来源:4-1 字节流之文件输入流FileInputStream-1

慕粉3170877

2016-07-09 16:08

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

写回答 关注

1回答

  • 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();
    	}


    慕粉3170...

    非常感谢!你理解的很透彻!

    2016-07-10 15:14:12

    共 1 条回复 >

文件传输基础——Java IO流

为您介绍IO流的使用,以及对象的序列化和反序列化的内容

133814 学习 · 1058 问题

查看课程

相似问题