バイト配列ストリーム

9758 ワード

バイト配列入力ストリーム
/**
* @author   -     
* @version     :2019 6 12    12:46:16
*        
* 1.    :        
* 2.   
* 3.  
* 4.     
*/

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOTest07 {
	public static void main(String[] args) {
		// 1    
		byte[] src = "askfhkajhfajf".getBytes();
		// 2   io
		InputStream is = null;
		// 3   
		
			is = new ByteArrayInputStream(src);
			int len = -1;//    
			byte[] flush = new byte[5];//    
			try {
				while((len=is.read(flush))!=-1) {
					String str = new String(flush, 0, len);
							System.out.println(str);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		
		// 4  
	}

}


バイト配列出力ストリーム
import java.io.ByteArrayOutputStream;

/**
* @author   -     
* @version     :2019 6 12    1:02:33
*        
* ByteArrayOutputStream
* 1.        
* 2.   :    
* 3.  (    )
* 4.    :    
* 
*       toByteArray()
*/
public class IOTest08 {
	public static void main(String[] args) {
		//1.   
		byte[] dest = "attack".getBytes();
		//2.  io 
		ByteArrayOutputStream baos = null;//           
		
		try {
			baos = new ByteArrayOutputStream();
			//3.  
			String msg = "     ";
			byte[] datas = msg.getBytes();
			baos.write(datas,0,datas.length);
			baos.flush();
			//    
			dest = baos.toByteArray();//      
			System.out.println(dest.length + "-->" +new String(dest,0,baos.size()));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		//4.close
		
	}
}