文件,byte[],HexString 之间的转换

来源:互联网 发布:成吉思汗征服月球知乎 编辑:程序博客网 时间:2024/04/29 06:49
Java代码 复制代码 收藏代码
  1. import java.io.BufferedOutputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. /**
  9. *
  10. * 文件,byte[],HexString 之间的转换
  11. *
  12. */
  13. public class FileDataConvert {
  14. public static void main(String[] args) {
  15. String srcFilePath = "d:/testFrom.png";
  16. String outFilePath = "d:/";
  17. String outFileName = "testTo.png";
  18. byte[] bytes = getBytesFromFile(srcFilePath);
  19. String str = bytes2HexString(bytes);
  20. System.out.println(str);
  21. byte[] bytes2 = hexString2Bytes(str);
  22. saveBytes2File(bytes2, outFilePath, outFileName);
  23. }
  24. /**
  25. * 从文件中获取byte数组
  26. */
  27. public static byte[] getBytesFromFile(String filePath) {
  28. byte[] buffer = null;
  29. try {
  30. File file = new File(filePath);
  31. FileInputStream fis = new FileInputStream(file);
  32. ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
  33. byte[] b = new byte[1024];
  34. int n;
  35. while ((n = fis.read(b)) != -1) {
  36. bos.write(b, 0, n);
  37. }
  38. fis.close();
  39. bos.close();
  40. buffer = bos.toByteArray();
  41. } catch (FileNotFoundException e) {
  42. e.printStackTrace();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. return buffer;
  47. }
  48. /**
  49. * 根据byte数组生成文件
  50. */
  51. public static void saveBytes2File(byte[] bfile, String filePath, String fileName) {
  52. BufferedOutputStream bos = null;
  53. FileOutputStream fos = null;
  54. File file = null;
  55. try {
  56. File dir = new File(filePath);
  57. if (!dir.exists() && dir.isDirectory()) {
  58. dir.mkdirs();
  59. }
  60. file = new File(filePath + "\\" + fileName);
  61. fos = new FileOutputStream(file);
  62. bos = new BufferedOutputStream(fos);
  63. bos.write(bfile);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. } finally {
  67. if (bos != null) {
  68. try {
  69. bos.close();
  70. } catch (IOException e1) {
  71. e1.printStackTrace();
  72. }
  73. }
  74. if (fos != null) {
  75. try {
  76. fos.close();
  77. } catch (IOException e1) {
  78. e1.printStackTrace();
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * 从字节数组到十六进制字符串转换
  85. */
  86. public static String bytes2HexString(byte[] b) {
  87. byte[] buff = new byte[2 * b.length];
  88. for (int i = 0; i < b.length; i++) {
  89. buff[2 * i] = hex[(b[i] >> 4) &0x0f];
  90. buff[2 * i + 1] = hex[b[i] &0x0f];
  91. }
  92. return new String(buff);
  93. }
  94. /**
  95. * 从十六进制字符串到字节数组转换
  96. */
  97. public static byte[] hexString2Bytes(String hexstr) {
  98. byte[] b = new byte[hexstr.length() / 2];
  99. int j = 0;
  100. for (int i = 0; i < b.length; i++) {
  101. char c0 = hexstr.charAt(j++);
  102. char c1 = hexstr.charAt(j++);
  103. b[i] = (byte) ((parse(c0) << 4) | parse(c1));
  104. }
  105. return b;
  106. }
原创粉丝点击