文件,byte[],HexString 之间的转换
来源:互联网 发布:成吉思汗征服月球知乎 编辑:程序博客网 时间:2024/04/29 06:49
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- *
- * 文件,byte[],HexString 之间的转换
- *
- */
- public class FileDataConvert {
- public static void main(String[] args) {
- String srcFilePath = "d:/testFrom.png";
- String outFilePath = "d:/";
- String outFileName = "testTo.png";
- byte[] bytes = getBytesFromFile(srcFilePath);
- String str = bytes2HexString(bytes);
- System.out.println(str);
- byte[] bytes2 = hexString2Bytes(str);
- saveBytes2File(bytes2, outFilePath, outFileName);
- }
- /**
- * 从文件中获取byte数组
- */
- public static byte[] getBytesFromFile(String filePath) {
- byte[] buffer = null;
- try {
- File file = new File(filePath);
- FileInputStream fis = new FileInputStream(file);
- ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
- byte[] b = new byte[1024];
- int n;
- while ((n = fis.read(b)) != -1) {
- bos.write(b, 0, n);
- }
- fis.close();
- bos.close();
- buffer = bos.toByteArray();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return buffer;
- }
- /**
- * 根据byte数组生成文件
- */
- public static void saveBytes2File(byte[] bfile, String filePath, String fileName) {
- BufferedOutputStream bos = null;
- FileOutputStream fos = null;
- File file = null;
- try {
- File dir = new File(filePath);
- if (!dir.exists() && dir.isDirectory()) {
- dir.mkdirs();
- }
- file = new File(filePath + "\\" + fileName);
- fos = new FileOutputStream(file);
- bos = new BufferedOutputStream(fos);
- bos.write(bfile);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (bos != null) {
- try {
- bos.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
- /**
- * 从字节数组到十六进制字符串转换
- */
- public static String bytes2HexString(byte[] b) {
- byte[] buff = new byte[2 * b.length];
- for (int i = 0; i < b.length; i++) {
- buff[2 * i] = hex[(b[i] >> 4) &0x0f];
- buff[2 * i + 1] = hex[b[i] &0x0f];
- }
- return new String(buff);
- }
- /**
- * 从十六进制字符串到字节数组转换
- */
- public static byte[] hexString2Bytes(String hexstr) {
- byte[] b = new byte[hexstr.length() / 2];
- int j = 0;
- for (int i = 0; i < b.length; i++) {
- char c0 = hexstr.charAt(j++);
- char c1 = hexstr.charAt(j++);
- b[i] = (byte) ((parse(c0) << 4) | parse(c1));
- }
- return b;
- }
- 文件,byte[],HexString 之间的转换
- String byte HexString的转换
- java中String byte HexString的转换
- java中String byte HexString的转换
- java中String byte HexString的转换
- Python hexstring-list-str之间的转换
- 文件 Stream 和 byte[] 之间的转换
- 文件和byte数组之间相互转换
- C# 中将自定义的structure转换为byte[] 及Stream 和 byte[] 之间的转换(文件流的应用)
- Java byte[] 和 HexString 互转
- string与byte[]之间的转换
- Image 和byte[]之间的转换
- Stream 和 byte[] 之间的转换
- 字符串与byte[]之间的转换
- Stream 和 byte[] 之间的转换
- Stream 和 byte[] 之间的转换
- String 与Byte[]数组之间的转换
- byte[]和String类型之间的转换
- 路径疑问
- python3:实现字符串的全排列
- device.cpp
- AESEncrypt.h
- 2012.6.28-29一级建造师-法规精讲(陈印老师)
- 文件,byte[],HexString 之间的转换
- java CRC16特殊实现方式
- 开源 免费 java CMS - 用户手册1.1发布 .
- 线段树-入门
- 大四,软件工程专业,求java实习机会,期望地区在上海,男
- 营业员小姐由于工作失误_JAVA面试题及答案
- 某手机厂家由于设计失误_JAVA面试题及答案
- HashMap和Hashtable的区别_JAVA面试题及答案
- Java编程思想中的一个关于类实例化的说明