`
bcworld
  • 浏览: 39285 次
  • 性别: 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};
}
 
分享到:
评论

相关推荐

    Java字节流 .pdf

    Java字节流 Java字节流是一种用于处理字节数据的流处理机制,在Java中,字节流不包含边界数据的连续流,字节流是由字节组成的,Java里字符由两个字节组成。字节流是最基本的,它是按字节来处理二进制数据。 字节流...

    Java字符流与字节流区别

    Java 流在处理上分为字符流和字节流。字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符、字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组。 Java 内用 Unicode 编码存储字符,字符流...

    Java字节流数据逐行读取(readLine)

    在Java编程中,字节流(Byte Stream)是处理数据的一种基本方式,它适用于任何类型的数据,包括文本、图片、音频等。字节流主要分为输入流和输出流,其中InputStream类是所有字节输入流的基类,而OutputStream类则是...

    Java字符流和字节流

    ### Java字符流和字节流详解 #### 一、引言 在Java中,I/O流主要用于处理输入输出操作,包括文件读写等。根据处理数据类型的不同,Java I/O流主要分为字节流和字符流两大类。字节流处理的是8位的字节数据,而字符流...

    Java实现字节流与图片的转化

    本教程将详细讲解如何使用Java实现字节流来转换和处理图片。 首先,我们需要理解字节流的概念。在Java中,字节流分为两种类型:输入字节流(InputStream)和输出字节流(OutputStream)。它们分别用于读取和写入...

    JAVA 字符流与字节流

    在Java编程语言中,输入/输出(I/O)操作是处理数据流的关键部分,而字符流与字节流则是实现这些操作的两种基本方式。理解它们的区别和应用场景对于任何Java开发者来说都是至关重要的。 ### 字节流 字节流是最基本...

    Java字节流与字符流的介绍.pdf

    Java 字节流与字符流的介绍 Java 中的流操作可以分为两种:字节流和字符流。字节流是指以 byte 为单位进行读写操作的流,而字符流是指以 char 为单位进行读写操作的流。 字节流的介绍 字节流的所有读操作都继承自...

    ByteIO_java_字节流_

    在Java编程语言中,字节流(Byte Stream)是I/O操作的基础,它处理的是单个字节的数据。...通过学习和理解这段代码,开发者可以深入理解Java字节流的工作原理,提升在处理二进制数据和网络通信时的能力。

    java字节流操作帮助文档

    java字节流相关操作,int转换成byte字节数组,String转换为byte字节数组,bcd码转换。

    Java IO 字节流 字符流

    Java IO系统分为两大类:字节流和字符流。字节流主要用于处理任何类型的原始数据,如图片、音频文件或者二进制文件,而字符流则专注于处理基于文本的数据。本文将深入探讨这两类流的原理、用途以及它们之间的关联。 ...

    Java中对于字节流的读取方式

    在Java编程语言中,字节流(Byte Stream)是处理数据的基本方式,特别是在进行I/O操作时。字节流主要用于处理二进制数据,如图片、音频、视频等非文本文件,同时也适用于处理文本文件。本篇文章将深入探讨Java中字...

    Java实现字节流与图片的转化Java源码

    本教程将详细讲解如何使用Java实现字节流与图片之间的转换,这对于在网络上传输或者存储图片等二进制数据至关重要。 首先,我们需要了解Java中的字节流。Java提供了两种类型的字节流:输入流(InputStream)和输出...

    Java IO字符流和字节流

    ### Java IO字符流和字节流详解 #### 一、引言 在Java编程中,输入输出(简称IO)操作是十分重要的一个方面。通过IO操作,我们可以读取文件、网络数据、用户输入等,也可以将数据写入到文件、网络、控制台等。Java...

    Java中的字节流.

    ### Java中的字节流 #### 一、字节流简介 在Java中,字节流是一种处理二进制数据的基本方式。它通过一系列类来实现数据的读取和写入功能,这些类主要继承自`InputStream`和`OutputStream`两个抽象类。字节流非常...

    java字节流和字符流[整理].pdf

    mo3 { public static void main(String[] args) throws IOException { File file = new File("d:" + File.separator + "demo.txt");...同时,转换流的使用能让我们在字节流和字符流之间灵活转换,解决编码问题。

    java字节流和字符流

    总的来说,Java的IO操作包括了对文件的打开、读写以及关闭等步骤,无论使用字节流还是字符流,都需要确保在操作完成后关闭流,以释放系统资源。流的关闭一般通过调用`close()`方法实现。在实际编程中,应该注意异常...

    Java基础11-(字节流、字符流)

    Java的IO流是按照数据的流向分为输入流和输出流,按照数据类型分为字节流和字符流。字节流处理的是二进制数据,适用于任何类型的文件,包括文本文件和非文本文件(如图片、音频、视频等)。字符流则用于处理字符数据...

    将图片以字节流存到数据库再从数据库获取显示

    例如,在Java中,可以使用`java.io.FileInputStream`和`java.nio`包中的`ByteBuffer`来读取图片文件,并将内容转化为字节流。 ```java File file = new File("image.jpg"); FileInputStream fis = new ...

    java 字节流

    javaIO读取实现 =初学者看看了解 希望有所帮助

    Java 字节流、字符流题目.pdf

    在Java中,流分为字节流和字符流两大类,它们都是抽象的概念,用于表示数据流动的方向。字节流处理的数据单位是字节,而字符流处理的是Unicode编码的字符。 一、字节流 字节流分为输入流和输出流,Java中最基础的...

Global site tag (gtag.js) - Google Analytics