package io;

import java.io.FileInputStream;
import java.io.IOException; public class IOUtil { /**
* 读取指定文件内容,按照16进制输出到控制台,为什么要按十六进制?
* 并且每输出10个byte换行
* @throws IOException
*/
public static void printHex(String filename) throws IOException{
//把文件作为字节流进行读操作
FileInputStream in = new FileInputStream(filename);//FileInputStream具体实现了在文件上读取数据
int b;
int i=1;
while((b=in.read())!=-1)//b=in.read()这就是程序核心
{
System.out.println(Integer.toHexString(b)+" ");//将整型b转换成16进制表示的字符串?输出
if(i++%10==0)
{
System.out.println();//每隔十个字节换行
}
}
in.close();
} }

思想就是:利用new FileInputStream(filename)把一个文件放入到一个输入流对象in中,调用in.read()方法来逐个字节读取(下面read源码),读取输入流一个字节的数据,返回一个整型int(那么是不是十六进制的整型呢?)。

再调用Integer.toHexString(b)方法将存在b的为int的一个字节的数据转换成一个字符串,然后输出。

总体原理就是,将一篇字符串 放入流进行单个字节的整型读取,然后再转换回字符串单个输出。

    /**
* Reads a byte of data from this input stream. This method blocks
* if no input is yet available.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* file is reached.
* @exception IOException if an I/O error occurs.
*/
public int read() throws IOException {
return read0();
} private native int read0() throws IOException;

//把文件作为字节流进行读操作
FileInputStream in = new FileInputStream(filename);//FileInputStream具体实现了在文件上读取数据

最新文章

  1. NC57,NC63-NC二开经验总结
  2. IOC框架整体介绍
  3. js 读写cookie。不同路径会储存各自的cookie。而 在v.net环境下读写是在 / 根目录。
  4. [ionic开源项目教程] - 第12讲 医疗模块的实现以及Service层loadMore和doRefresh的提取封装
  5. Spring项目的建立-移植流程(非入门教程)
  6. PHP第一章学习——了解PHP(下)
  7. fpga串口通信
  8. 实验吧 简单的SQL注入1
  9. 为 Confluence 6 分发包设置一个邮件会话
  10. app保存图片到用户相册时闪退解决办法
  11. 11个超震撼的HTML5和纯CSS3动画源码
  12. mysql数据库连接出问题,提示超时 java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.解决办法
  13. 重写alert方法,去掉地址显示
  14. (转)Xcode导航快捷键(大全)
  15. GitHub 代码上传
  16. 学习笔记36—坚果云 | Papership或Zotero使用webDAV验证服务器不成功怎么办?
  17. Spring-Session实现Session共享入门教程
  18. 【HNOI2015】落忆枫音
  19. Codeforces Round #500 (Div. 2) D - Chemical table
  20. java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)

热门文章

  1. 使用GitBook, Git github, Markdown 快速发布你的书籍
  2. SecureCRT 设置字体跟颜色
  3. 再理解 as3.0接口
  4. Angular1.0 在Directive中调用Controller的方法
  5. MyEclipse中Save could not be completed
  6. iOS 启动页放大淡出效果
  7. PHP如何进阶,提升自己
  8. poj 2524 Ubiquitous Religions(并查集)
  9. [Sdoi2014]数数[数位dp+AC自动机]
  10. EasyNVR无插件直播服务器软件接口调用返回“Unauthorized”最简单的处理方式