62,614
社区成员
发帖
与我相关
我的任务
分享
public static void outputFileWitHex(String path){
byte[] buffer = null;
FileInputStream fis = null;
int n;
String ret = "";
File file = new File(path);
try {
fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
String lineSeparator = (String) java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
for (int i = 0; i < buffer.length; i++) {
String hex = Integer.toHexString(buffer[i] & 0xFF)+" ";//每个字符后面添加空格.
if(i==15)
{
//hex+=lineSeparator;//兼容所有系统
/*
换行符提示:
windows:\r\n
linux:\n
*/
hex+="\r\n";//换行符,由于i从0开始,所以15个就换行.
}
if (hex.length() == 1) {
hex = "0x" + hex;
}
ret += hex.toUpperCase();
}
System.out.println(ret);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void outputFileWitHex(String path){
byte[] buffer = null;
FileInputStream fis = null;
int n;
String ret = "";
File file = new File(path);
try {
fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
for (int i = 0; i < buffer.length; i++) {
String hex = Integer.toHexString(buffer[i] & 0xFF);
if (hex.length() == 1) {
hex = "0x" + hex;
}
ret += hex.toUpperCase();
}
System.out.println(ret);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}