java之IO操作之 節點流
轉換流:位元組流轉字元流(處理亂碼)
二進位制(計算機) 解碼 字元(人類)
字元(人類) 編碼 二進位制(計算機)
亂碼原因:編碼與解碼字符集不一致 位元組缺少,長度丟失
package learn_java.io.others;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/*
*
* 位元組陣列節點流
*/
public class ByteArrayDemo01 {
public static void main(String[] args) throws IOException
{
// read() ;
// write();
read(write());
}
/*
*
* 輸入流與檔案輸入流一致
* 讀取位元組陣列
*/
public static void read(byte[] src) throws IOException
{
//
// String msg="輸入流與檔案輸入流一致";
//
// byte[] src=msg.getBytes();
// 選擇流(沒有新增方法,可以使用多型)
InputStream is =new BufferedInputStream(
new ByteArrayInputStream(src));//與外界沒有聯絡,故沒有異常,與檔案輸入流不同
//操作
byte[] flush =new byte[1024];
int len=0;
while(-1!=(len=is. read(flush))) {
System.out.println(new String(flush,0,len));
is.close();
}
}
public static byte [] write() throws IOException
{
byte[] dest;
//選擇流
ByteArrayOutputStream bos =new ByteArrayOutputStream();
//操作寫出到bos
String msg="操作時不能使用多型,因為有新的方法出現了";
byte[] info =msg.getBytes();
bos.write(info,0,info.length);
//獲取資料
dest=bos.toByteArray();
bos.close();
return dest;
}
}