`
bcworld
  • 浏览: 37838 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

java 字节流使用

阅读更多
package advance.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StreamTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		byte[]b=imageToByte("lock.gif");
//		for (int i = 0; i < b.length; i++) {
//			if (i%20==0) {
//				System.out.println();
//			}
//			System.out.print(b[i]+",");
//		}
		makeImage(b, System.currentTimeMillis()+".gif");
		//若无 lock.gif文件,可以用下面的字节数组
		makeImage(MyImage.image, System.currentTimeMillis()+".gif");
	}
	/**
	 * 将读取到的文件转换成字节数组
	 * @param filename
	 * @return
	 */
	public static byte[] imageToByte(String filename) {
		InputStream inputStream=null;
		ByteArrayOutputStream baos=null;
		try {
			inputStream=new FileInputStream(new File(filename));
			byte b[]=new byte[1024];
			int i=-1;
			baos=new ByteArrayOutputStream();
			while((i=inputStream.read(b))!=-1){
				baos.write(b, 0, i);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (baos!=null) {
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (inputStream!=null) {
				try {
					inputStream.close();
				} catch (Exception e2) {
				}
			}
		}
		return baos.toByteArray();
	}
	/**
	 * 根据字节数组产生文件
	 * @param source
	 * @param targetfile
	 */
	public static void makeImage(byte[] source,String targetfile) {
		OutputStream outputStream=null;
		try {
			outputStream=new FileOutputStream(new File(targetfile));
			outputStream.write(source);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (outputStream!=null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
class MyImage{
	public static final byte[] image={71,73,70,56,57,97,18,0,25,0,-9,0,0,-1,-5,-114,-108,-104,114,-34,
		-63,-124,-11,-20,-36,-21,-41,90,-3,-19,-124,-13,-23,-54,-1,-5,-109,-5,-4,-21,
		-21,-29,-84,-80,123,25,-128,115,75,-15,-29,-29,-41,-70,-122,79,70,60,-31,-54,
		-126,-23,-27,-103,83,78,66,-38,-73,107,-28,-86,41,-1,-15,-72,-14,-38,-87,-41,
		-63,106,-18,-26,-38,-21,-34,-68,-2,-28,99,-72,-128,24,-5,-46,92,-31,-56,114,
		-8,-4,-6,-70,-106,59,-3,-19,-116,-1,-14,-117,-3,-14,-101,-3,-12,-3,-20,-52,
		100,-37,-51,122,-34,-49,-86,-2,-38,92,-15,-43,123,-2,-4,-1,-26,-63,54,-103,
		-115,90,-43,-101,41,-30,-60,123,-12,-27,-101,-27,-62,-113,-21,-30,-70,-30,-51,-102,
		-29,-61,93,-17,-27,-54,-2,-43,99,-3,-30,116,-4,-16,-109,-21,-39,123,-2,-26,
		125,-2,-19,115,-2,-2,-99,-21,-36,-111,-13,-1,-4,-18,-43,115,-35,-81,53,-3,
		-13,-124,-36,-90,35,-2,-1,-11,-1,-2,-42,-29,-55,123,-10,-15,-47,-38,-45,-108,
		-20,-69,58,-4,-4,-2,-2,-9,-91,-1,-3,-13,-2,-3,-104,-2,-6,-3,-51,-91,
		34,-25,-75,46,-1,-2,-63,-13,-55,102,-4,-3,-4,-31,-62,101,-11,-15,-29,-59,
		-110,28,-14,-26,-68,-5,-2,-1,-9,-17,-106,-14,-22,-101,-50,-78,104,-1,-1,-4,
		-35,-53,-102,-35,-93,47,-5,-4,-1,-1,-1,-84,-13,-53,82,-34,-66,115,-12,-22,
		-91,-1,-2,-1,-31,-61,118,-4,-1,-2,-1,-2,-5,-4,-1,-7,-4,-4,-5,-1,
		-32,120,125,123,101,-19,-35,-60,-23,-57,71,-23,-38,-122,-37,-102,16,-29,-52,110,
		-7,-9,-35,-21,-37,-47,-25,-49,-92,-23,-53,126,-6,-17,-99,-18,-20,-50,-22,-42,
		-106,-41,-84,102,-27,-85,49,-1,-1,-56,-4,-35,117,-30,-80,61,-24,-80,53,-34,
		-79,68,-30,-42,-65,-4,-50,93,-16,-23,-81,-13,-43,105,-58,-106,34,-25,-39,115,
		-37,-90,55,-8,-1,-3,-2,-3,-6,-32,-57,77,-28,-43,109,-21,-33,111,-22,-43,
		-123,-6,-9,-65,-52,-96,43,-49,-84,40,-12,-68,62,-53,-51,123,-11,-18,85,-14,
		-35,-113,-8,-11,-21,-5,-7,-28,-16,-25,-111,-22,-28,127,-23,-44,-88,-10,-21,-113,
		-32,-67,102,-26,-49,96,-1,-10,127,-6,-5,-1,-5,-12,-45,-16,-26,111,-20,-55,
		-123,-4,-7,-5,-7,-18,99,-23,-70,80,-31,-64,80,-26,-51,85,-23,-58,81,-19,
		-58,89,-30,-45,-110,-27,-55,-65,-29,-39,-99,-63,-115,67,-28,-57,-121,-46,-69,95,
		-23,-72,66,-62,-115,17,-28,-80,68,-27,-77,72,-29,-69,66,-31,-68,79,-23,-13,
		-58,-18,-67,79,-3,-18,126,-2,-43,113,-2,-37,120,-25,-77,55,-39,-76,45,-32,
		-67,55,-16,-28,-58,-11,-19,-62,-5,-8,-11,-6,-1,-15,-37,-49,-117,-46,-87,47,
		-17,-54,108,-38,-87,47,-42,-57,-111,-46,-54,-99,-33,-53,-111,-15,-58,87,55,51,
		51,-14,-50,94,-19,-40,-97,-17,-41,-67,-57,-85,114,-13,-25,-46,-15,-25,119,-9,
		-45,101,-11,-2,-11,112,82,59,-13,-34,102,-59,-113,36,-5,-8,-102,-1,-7,-100,
		-15,-23,119,-27,-40,-91,-20,-43,-94,-48,-47,119,-17,-35,-82,-5,-1,-5,-41,-91,
		54,-5,-9,-88,-47,-89,58,-58,-67,-127,-20,-31,-58,-3,-25,-122,-45,-67,125,-38,
		-77,118,-25,-38,-55,-18,-42,-62,-20,-46,65,-19,-34,-51,-22,-49,115,-2,-25,108,
		-1,-1,-66,-13,-20,-82,-8,-19,-82,-6,-3,-1,-13,-63,69,-6,-1,-1,-7,-55,
		113,-34,-49,93,-4,-2,-5,-1,-3,-4,-1,-3,-7,-16,-31,-75,-27,-32,-114,-17,
		-28,-124,-17,-28,-115,-27,-60,68,-53,-88,66,-20,-60,74,-54,-95,88,-22,-47,106,
		-51,-79,80,-28,-42,-119,-56,-60,-122,-21,-33,-120,-5,-2,-58,-57,-99,20,-1,-1,
		-1,33,-7,4,0,0,0,0,0,44,0,0,0,0,18,0,25,0,0,8,
		-1,0,-33,9,-38,-79,76,-119,-120,1,-91,30,88,2,-91,47,10,-118,39,88,
		-2,-3,67,65,101,-121,24,36,104,118,-59,107,65,-87,0,63,96,23,-80,68,
		-4,7,70,-116,-70,49,9,-40,69,-125,64,-124,-56,-88,42,-110,118,61,-110,-8,
		15,8,-103,127,12,-124,52,2,84,66,78,-108,111,15,58,21,-70,64,83,23,
		16,17,-92,108,16,-128,-111,75,34,-89,55,-31,44,-71,1,35,-47,26,-106,1,
		-89,70,8,25,-128,2,-123,32,20,-58,82,-92,17,71,-11,31,58,48,47,8,
		-47,-101,-42,1,19,10,117,-24,-64,49,73,33,-125,-26,22,20,-43,108,-75,-86,
		-96,107,8,60,105,89,-68,-11,-88,101,1,23,16,20,-24,-80,20,-21,-95,-57,
		69,22,18,108,122,57,97,21,-56,-105,-65,124,-56,-34,2,-47,-95,-120,87,24,
		43,31,-72,-103,17,38,101,-59,10,13,30,-10,-128,121,-12,2,-43,-110,37,49,
		10,-48,80,-42,-91,-120,42,5,10,76,93,-39,102,-92,68,-73,122,82,-92,-4,
		-39,-80,97,-47,-124,31,-119,60,28,-61,112,-50,-128,-110,6,-107,-78,41,-48,112,
		-51,-45,42,45,120,92,-79,-7,18,4,-127,57,3,70,54,-7,-1,-7,116,-113,
		78,-116,74,44,14,-75,-8,-46,-60,78,-65,38,86,100,-108,-127,-45,-52,-46,28,
		10,85,-30,32,122,21,32,64,16,68,-40,-28,16,-119,12,70,-100,-46,-123,40,
		58,112,113,-60,17,57,64,-32,-64,25,77,64,19,66,18,-13,-36,82,96,58,
		-80,-88,-111,67,8,33,28,0,65,4,103,112,1,64,13,-49,-56,51,69,25,
		44,20,-111,-118,13,73,-44,80,-61,1,-5,56,-32,0,35,73,-128,112,64,50,
		125,60,17,6,19,-76,-40,0,-64,7,5,0,-96,-62,48,17,80,3,64,1,
		32,104,-46,71,59,97,76,-112,7,15,62,-36,112,-61,37,-38,60,120,0,14,
		55,-128,96,72,2,79,120,-15,67,29,60,-60,98,6,13,56,56,-30,-64,2,
		0,-112,115,3,55,28,96,-16,-124,4,107,76,48,78,1,-77,-36,-111,-63,9,
		-52,-88,112,73,6,-54,-60,-16,0,87,2,56,-93,5,49,25,-52,32,-117,9,
		38,72,32,4,62,80,-80,80,76,38,99,24,49,68,55,30,-40,99,2,31,
		-21,56,17,-54,8,28,4,-125,65,20,-125,-48,-124,68,27,-65,92,-31,-123,23,
		2,-64,48,-55,20,67,64,-126,4,25,-19,-120,3,33,81,64,0,59};
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics