字节,字节数组输入输出流ByteArrayInputStream,ByteArrayOutputStream理解

字节作为数据的一种表现形式可以与其他的数据表现形式转换

1 字节理解 

2 字节与人类语言字符串转换 (new String(byte[] s) ,String.toBytes() )

3  文件与字节转换

从另一方面讲,任何东西都可以转为最小单位字节,然后做最终处理。

以下自己学习得到的观点,有错误的地方请指出

在通常操作用,字节接触的比较少,这里对于字节的理解进行深入学习

   1 字节是网络传输的最小单元(个人认为,自己最重要的作用,网络传输的数据包可能就是字节)

   2 网络传输过后要对字节进行编码(即不同语言规定几个字节为一个字符切分)

   3 上面得到字节序列,组合即为字符串(字符串通常是我们人所看的语言)

ByteArrayInputStream

ByteArrayOutputStream  

字节输入流和字节输出流 

1 因为网络传输的过程中是字节,有的时候我们可以用字节输入流来保存传输过程的输入包,然后切分字节使用 

2 有时候需要将一个文件变成字节处理,可以用将数据写到字节输出流中 

文件变为字节:

  

FileInputStream fis=new FileInputStream("test");
BufferedInputStream bis=new BufferedInputStream(fis);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int c=bis.read();//读取bis流中的下一个字节
while(c!=-1){
     baos.write(c);
     c=bis.read();
}
bis.close();
byte retArr[]=baos.toByteArray(); 

流的使用:以下为个人理解

流分类:节点流和处理流 

节点流为直接与索要读取数据终端链接的流(如FileInputStream:直接从文件中读取,ByteArrayOutputStream : 直接从字节数组中读取,ObjectInputStream:直接从Object中读取等等 )

处理流为在节点流基础上添加新功能的流:(如DataInputStream 使流可以直接读出各种类型的数据,BufferedInputStream :使流具有缓冲功能,读取速度变快。)

一些测试代码:

字节流的使用:

	//字节输入输出 
	private static void testByteArrayStram() throws IOException{
		
		String inputStr = "我叫 唐三藏";
		byte[] bs = inputStr.getBytes(); 
		ByteArrayInputStream is = new ByteArrayInputStream(bs);
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		long start = System.currentTimeMillis();
		byte[]  buf = new byte[1024];
		int len=0 ; 
		long start1 = System.currentTimeMillis();
		while ( (len=is.read(buf, 0, buf.length))!=-1) {
//			 System.out.println( new String(buf ));
			 os.write( buf);
		}
		System.out.println(start1-System.currentTimeMillis()  );
		os.write(bs ); 
		System.out.println( os.toString());
//		System.out.println( os.);	
		
	}

字节流带有缓冲区:

	//带有缓冲流的 
	private static void testFileInput() throws IOException{
		FileInputStream fis = new FileInputStream(new File("E:\\study\\Hadoop权威指南++中文版.pdf"));
		BufferedInputStream bis = new BufferedInputStream( fis ); 
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		byte[] buf = new byte[1024];
		long len = 0 ; 
		long start  = System.currentTimeMillis() ; 
		while((len=bis.read(buf, 0, buf.length))!=-1){
			bos.write( buf ) ;
		}
		System.out.println(  System.currentTimeMillis() -start );
//		System.out.println( bos.toString() ); 
		bis.close() ;
		bos.close() ;
		 
	}

  

文件流使用:

	//无缓冲流的 
	private static void testFileInput2() throws IOException{
		FileInputStream fis = new FileInputStream(new File("E:\\study\\线程笔记20140606.docx"));
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
//		FileOutputStream out = new FileOutputStream(new File("E:\\study\\线程笔记1测试.docx") );
		
		byte[] buf = new byte[1024];
		long len = 0 ; 
		long start  = System.currentTimeMillis() ; 
		while((len=fis.read(buf, 0, buf.length))!=-1){
			bos.write( buf ) ;
		}
		System.out.println(  System.currentTimeMillis() -start );
//		System.out.println( bos.toString() ); 
//		bos.writeTo( out ) ; //写入到 字节输出流中的字节,在写到另一个文档中不是乱码
		
		fis.close() ;
		bos.close() ;
//		out.close();
	}	

 参考:http://www.cnblogs.com/shitouer/archive/2012/12/19/2823641.html

猜你喜欢

转载自username2.iteye.com/blog/2200364