您的位置:首页 > 编程语言 > Java开发

Java中FileInputStream读写数据时换行处理

2016-04-01 08:36 411 查看
上一篇博客中写到了关于Java中流的一些读取方法。这里要写的具体地一点代码。就是将读取到的内容进行16进制的输出,并且做适当的换行处理。

直接上代码:

public void printHex(String name) throws IOException {
FileInputStream fis = new FileInputStream(name);
int by;
int i = 1;
while ((by = fis.read()) != -1) {
// 单位数前补0
if (by <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(by) + "\t");
// 每输出10个换行
if (i++ % 10 == 0) {
System.out.println();
}
}

}


在main方法中调用,并输出:

FileMethod fm = new FileMethod();
try {
fm.printHex("e:/my.txt");
} catch (IOException e) {
e.printStackTrace();
}


特别要记得的是一个判断:

// 单位数前补0
if (by <= 0xf) {
System.out.print("0");
}


That’s so easy!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java