将文件转化为字节数组
写了一个方法,直接用即可
/**
* 将文件转化成字节数组
*
* @param 要转换的文件路径
* @return 转换成的字节数组
* @throws IOException
*/
public static byte[] fileToByteArray(String filePath) throws IOException {
// 创建源与目的地
File fromfile = new File(filePath);
// 选择流
FileInputStream instream = new FileInputStream(fromfile);
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
// 操作
byte[] flush = new byte[1024 * 10];
int len = -1;
while ((len = instream.read(flush)) != -1) {
outstream.write(flush, 0, len);
}
// 关闭流
outstream.flush();
instream.close();
return outstream.toByteArray();
}
将字符串转化为字节数组
这个比较简单,一行就完事
byte[] data = "这是一个字符串".getBytes()
将文件转化为十六进制字节数组的工具——输出效果(部分)
00000: 69 6D 70 6F 72 74 20 6A 61 76 61 2E 69 6F 2E 42
00010: 79 74 65 41 72 72 61 79 4F 75 74 70 75 74 53 74
00020: 72 65 61 6D 3B 0D 0A 69 6D 70 6F 72 74 20 6A 61
00030: 76 61 2E 69 6F 2E 46 69 6C 65 3B 0D 0A 69 6D 70
00040: 6F 72 74 20 6A 61 76 61 2E 69 6F 2E 46 69 6C 65
00050: 49 6E 70 75 74 53 74 72 65 61 6D 3B 0D 0A 69 6D
00060: 70 6F 72 74 20 6A 61 76 61 2E 69 6F 2E 46 69 6C
00070: 65 4F 75 74 70 75 74 53 74 72 65 61 6D 3B 0D 0A
00080: 69 6D 70 6F 72 74 20 6A 61 76 61 2E 69 6F 2E 49
00090: 4F 45 78 63 65 70 74 69 6F 6E 3B 0D 0A 69 6D 70
000A0: 6F 72 74 20 6A 61 76 61 2E 69 6F 2E 4F 75 74 70
000B0: 75 74 53 74 72 65 61 6D 3B 0D 0A 0D 0A 70 75 62
000C0: 6C 69 63 20 63 6C 61 73 73 20 4D 61 69 6E 20 7B
000D0: 0D 0A 0D 0A 09 2F 2F 20 28 E7 A1 AC E7 9B 98 E4
000E0: B8 AD 29 E5 9B BE E7 89 87 20 2D 3E 20 28 E5 86
000F0: 85 E5 AD 98 E4 B8 AD 29 E5 AD 97 E8 8A 82 E6 95
00100: B0 E7 BB 84 0D 0A 09 70 75 62 6C 69 63 20 73 74
00110: 61 74 69 63 20 62 79 74 65 5B 5D 20 66 69 6C 65
00120: 54 6F 42 79 74 65 41 72 72 61 79 28 53 74 72 69
00130: 6E 67 20 66 69 6C 65 50 61 74 68 29 20 74 68 72
00140: 6F 77 73 20 49 4F 45 78 63 65 70 74 69 6F 6E 20
工具代码
源码来自《JAVA编程思想》并稍加修改。留着备用~
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
/**
* 将文件转化成字节数组
*
* @param 要转换的文件路径
* @return 转换成的字节数组
* @throws IOException
*/
public static byte[] fileToByteArray(String filePath) throws IOException {
// 创建源与目的地
File fromfile = new File(filePath);
// 选择流
FileInputStream instream = new FileInputStream(fromfile);
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
// 操作
byte[] flush = new byte[1024 * 10];
int len = -1;
while ((len = instream.read(flush)) != -1) {
outstream.write(flush, 0, len);
}
// 关闭流
outstream.flush();
instream.close();
return outstream.toByteArray();
}
/**
* 将字节数组转换为十六进制,以优美的格式输出
*
* @param data
* @return
*/
public static String format(byte[] data) {
StringBuilder result = new StringBuilder();
int n = 0;
for (byte b : data) {
if (n % 16 == 0)
result.append(String.format("%05X: ", n));
result.append(String.format("%02X ", b));
n++;
if (n % 16 == 0)
result.append("\n");
}
result.append("\n");
return result.toString();
}
public static void main(String[] args) throws Exception {
// System.out.println(format("abcdefg".getBytes()));//将字符串转换为字节数组输出
System.out.println(format(fileToByteArray("src/Main.java")));// 将文件转化为字节数组输出
}
}