(一)、 public class ByteArrayInputStream extends InputStream;
ByteArrayInputSteam:該類是InputStream的子類,它從內存中的字節數組中讀取數據,因此它的數據源是一個字節數組。這個類的構造方法包括:
ByteArrayInputStream(byte[] buf)--------參數buf指定字節數組類型的數據源。
ByteArrayInputStream(byte[] buf, int offset, int lenght)-----參數buf指定字節數組類型數據源,參數offset指定從數組中開始讀取數據的起始下標位置,lenght指定從數組中讀取的字節數。
ByteArrayInputStream類本身采用了適配器設計模式,它把字節數組類型轉換為輸入流類型,使得程序能夠對字節數組進行讀操作。
public static void byteArrTest() throws IOException{
ByteArrayInputStream bis=new ByteArrayInputStream("abcd".getBytes());
int len=0;
while((len=bis.read())!=-1){
System.out.println((char)len+"<=>"+len);
}
bis.close();
}
(二)、public class ByteArrayOutputStream extends OutputStream。
ByteArrayOutputStream:此類實現了一個輸出流,其中的數據被寫入一個 byte 數組。緩沖區會隨著數據的不斷寫入而自動增長。可使用 toByteArray() 和 toString() 獲取數據。關閉 ByteArrayOutputStream 無效。此類中的方法在關閉此流後仍可被調用,而不會產生任何 IOException。
1、public byte[] toByteArray()創建一個新分配的 byte 數組。其大小是此輸出流的當前大小,並且緩沖區的有效內容已復制到該數組中
public static void byteArrOut() throws IOException{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bos.write(97);
bos.write(new byte[]{98,99,100});
//public byte[] toByteArray()創建一個新分配的 byte 數組。其大小是此輸出流的當前大小,並且緩沖區的有效內容已復制到該數組中。
byte [] bys=bos.toByteArray();
for(byte b:bys){
System.out.println(b+"<==>"+(char)b);
}
}
2、public String toString()使用平台默認的字符集,通過解碼字節將緩沖區內容轉換為字符串。新 String 的長度是字符集的函數,因此可能不等於緩沖區的大小。
public static void byteArrOut2() throws IOException{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bos.write(97);
bos.write(new byte[]{98,99,100});
//public String toString()使用平台默認的字符集,通過解碼字節將緩沖區內容轉換為字符串。新 String 的長度是字符集的函數,因此可能不等於緩沖區的大小。
String str=bos.toString();
System.out.println(str);//abcd
}
(三)、ByteArrayInputStream、ByteArrayOutputStream一起使用
public static void byteArrOut() throws IOException{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bos.write(97);
bos.write(new byte[]{98,99,100});
byte [] bys=bos.toByteArray();
ByteArrayInputStream bis=new ByteArrayInputStream(bys);
int len=0;
while((len=bis.read())!=-1){
System.out.print((char)len);//abcd
}
}
PrintStream是FilterOutputStream的子類。
PrintWriter 是 Writer的子類。
打印流的特點:
* A:只有寫數據的,沒有讀取數據。只能操作目的地,不能操作數據源。打印流只有字節打印流PrintStream 與字符打印流PrintWriter
* B:可以操作任意類型的數據。他們的print()系列方法與println()系列方法可以操作任意類型數據。
* C:如果啟動了自動刷新,能夠自動刷新。但必須調用println()方法
PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);
PrintStream ps=new PrintStream(new FileOutputStream("a.txt"),true);
* D:該流是可以直接操作文本文件的。
public PrintStream(File file) throws FileNotFoundException。
public PrintWriter(File file) throws FileNotFoundException。
1、使用PrintStream實現文件的copy
public static void streamCopy() throws IOException{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("a.txt"));
//public PrintStream(OutputStream out,boolean autoFlush)使用該夠造函數 開啟自動刷新功能
PrintStream ps=new PrintStream(new FileOutputStream("Copy.java"),true);
byte [] bys=new byte[1024];
int line =0;
while((line=bis.read(bys))!=-1){
ps.println(new String(bys,0,line));
}
ps.close();
bis.close();
}
2、使用 PrintWriter實現文件的copy
public static void copy() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封裝目的地
//public PrintWriter(OutputStream out,boolean autoFlush);使用該夠造函數 開啟自動刷新功能
PrintWriter pw =new PrintWriter(new FileWriter("Copy.java"), true);
String line = null;
while((line=br.readLine())!=null){
pw.println(line);
}
pw.close();
br.close();
}
System類中的三個成員變量
//“標准”輸入流。此流已打開並准備提供輸入數據。通常,此流對應於鍵盤輸入或者由主機環境或用戶指定的另一個輸入源。
public final static InputStream in = null;
//“標准”輸出流。此流已打開並准備接受輸出數據。通常,此流對應於顯示器輸出或者由主機環境或用戶指定的另一個輸出目標。
public final static PrintStream out = null;
//“標准”錯誤輸出流。此流已打開並准備接受輸出數據。 通常,此流對應於顯示器輸出或者由主機環境或用戶指定的另一個輸出目標
public final static PrintStream err = null;
由System.out變量我們可以:
// 有這裡的講解我們就知道了,這個輸出語句其本質是IO流操作,把數據輸出到控制台。
System.out.println("helloworld");
// 獲取標准輸出流對象
PrintStream ps = System.out;
ps.println("helloworld");