将文件流转换成String,然后将转换后的String再转换成文件流

 

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/***
 * description: 对流数据的操作 
 * @author xyc 创建时间:2015-6-4  
 * @Copyright(c)2014:北京哈Q娱乐传媒有限公司
 */
public class ReadIo2Str {

	private static ReadIo2Str instance;

	private ReadIo2Str() {
	}

	public static ReadIo2Str getInstance() {
		if (null == instance) {
			instance = new ReadIo2Str();
		}
		return instance;
	}

	public static void main(String[] args) {
		String filePath = "C:\\Users\\yxcui\\Desktop\\test03.png";
		String filePath2 = "C:\\Users\\<span style="font-family: Arial, Helvetica, sans-serif;">yxcui</span><span style="font-family: Arial, Helvetica, sans-serif;">\\Desktop\\test0322.png";</span>
		String str = ReadIo2Str.getInstance().read2String(filePath);
		ReadIo2Str.getInstance().read2Io(str, filePath2);
		System.out.println(str);
	}

	/**
	 * description:  将流转换成String
	 * @param filePath
	 * @return  
	 * @author xyc 
	 * @update 2015-6-4
	 */
	public String read2String(String filePath) {
		String ioStr = "";
		FileInputStream is = null;
		try {
			is = new FileInputStream(filePath);
			byte[] in = new byte[is.available()];
			is.read(in);
			ioStr = HQCodec.hexEncode(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != is) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return ioStr;
	}

	/**
	 * description:  将文件流写到某一个目录下
	 * @param ioStr
	 * @param filePath  
	 * @author xyc 
	 * @update 2015-6-4
	 */
	public void read2Io(String ioStr, String filePath) {
		FileOutputStream fos = null;
		try {
			byte[] iobyte = HQCodec.hexDecode(ioStr);
			File file = new File(filePath);
			if (file.exists()) {
				file.delete();
			}
			fos = new FileOutputStream(file);
			fos.write(iobyte, 0, iobyte.length);
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 

 

 

 

 

 

/**
 * description:  hexcode转换
 * @author xyc 创建时间:2015-6-4  
 * @Copyright(c)2014:北京哈Q娱乐传媒有限公司
 */
public class HQCodec {

	static final char[] HEX = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
			'F' };

	public static String hexEncode(byte[] buffer) {
		if (buffer.length == 0) {
			return "";
		}
		int holder = 0;
		char[] chars = new char[buffer.length * 2];
		for (int i = 0; i < buffer.length; i++) {
			holder = (buffer[i] & 0xf0) >> 4;
			chars[i * 2] = HEX[holder];
			holder = buffer[i] & 0x0f;
			chars[(i * 2) + 1] = HEX[holder];
		}
		return new String(chars);
	}

	public static byte[] hexDecode(String hex) {
		//A null string returns an empty array
		if (hex == null || hex.length() == 0) {
			return new byte[0];
		} else if (hex.length() < 3) {
			return new byte[] { (byte) (Integer.parseInt(hex, 16) & 0xff) };
		}
		//Adjust accordingly for odd-length strings
		int count = hex.length();
		int nibble = 0;
		if (count % 2 != 0) {
			count++;
			nibble = 1;
		}
		byte[] buf = new byte[count / 2];
		char c = 0;
		int holder = 0;
		int pos = 0;
		for (int i = 0; i < buf.length; i++) {
			for (int z = 0; z < 2 && pos < hex.length(); z++) {
				c = hex.charAt(pos++);
				if (c >= 'A' && c <= 'F') {
					c -= 55;
				} else if (c >= '0' && c <= '9') {
					c -= 48;
				} else if (c >= 'a' && c <= 'f') {
					c -= 87;
				}
				if (nibble == 0) {
					holder = c << 4;
				} else {
					holder |= c;
					buf[i] = (byte) holder;
				}
				nibble = 1 - nibble;
			}
		}
		return buf;
	}

}

 

 

 

 

 

QQ群:100162042

 

更多JAVA教程,可访问:http://www.ijson.net/

 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值