快到周末了,Team里的空气也变得慵懒起来,大概它在为我们轻松的周末提前做准备了。
看了几片文档,突然觉着无聊起来。启动Eclipse想些点什么,脑子里突然冒出写个HexDumper的想法。基本想法是,这个小冬冬简单,能在下班前写好。实际写下来发现,我是过高估计了这个程序的难度,只用了10分钟,就搞定,并测试通过:-).
(纯做闲时解闷用,算不上产品级)
import java.io.*;
public class HexDumper {
public static void main(String[] args)
{
try
{
final int LEN = 1024;
final int CHARS_OF_LINE = 16;
byte buffer[] = new byte[LEN];
String filename = "input.dat";
FileInputStream reader = new FileInputStream(filename);
PrintWriter writer = new PrintWriter(new FileOutputStream("output.dat"));
int len = 0;
int tmpLen = 0;
int index = 0;
while(reader.available() > 0)
{
tmpLen = reader.read(buffer, 0, LEN);
if(tmpLen == -1)
break;
else
{
index -= len;
int res = tmpLen % CHARS_OF_LINE;
int lines = (tmpLen - res)/CHARS_OF_LINE;
for(int i=0; i {
writer.format("%08d\t",index);
for(int j=0; j<16; j++)
{
writer.format(" %02X", buffer[index++]);
}
writer.format("\t%s\n", new String(buffer,index-CHARS_OF_LINE,CHARS_OF_LINE,"UTF-8"));
}
len += tmpLen;
}
}
writer.flush();
System.out.println("Total characters : " + len);
reader.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
阅读(1784) | 评论(0) | 转发(0) |