在I/O類庫中,java.io.InputStream和java.io.OutputStream分別表示字節輸入流和字節輸出流,它們都是抽象類,不能實例化,數據流中的最小單位是字節,所以叫做字節流。
一、InputStream中的讀取數據的方法如下:
1 、int read()
功能:讀取一個字節的數據,並且返回讀到得數據,如果返回-1,則表示讀到輸入流的末尾。
2、int read(byte[] b)
功能:從輸入流中讀取一定量的字節,並將其存儲在字節數組b中,返回實際讀取的字節數,如果返回-1,則表示讀到輸入流的末尾。
3、int read(byte[] b, int off, int len)
功能:將數據讀入一個字節數組,同時返回讀取的實際字節數,如果返回-1,則表示讀到輸入流的末尾。off指定在數組b中存放數據的起始偏移位置,len指定讀取的最大字節數。
4、available()
功能:返回此輸入流下一個方法調用可以不受阻塞地從此輸入流讀取或跳過的估計字節數。
5、close()
功能:關閉輸入流,釋放這個流的相關資源。
二、OutputStream中寫入數據的方法如下:
1 、int write(int b)
功能:將b的最低的一個字節寫入此輸入流,其他三個字節丟棄。
2、int write(byte[] b)
功能:將指定的字節數組b寫入此輸入流。
3、int write(byte[] b, int off, int len)
功能:將指定byte數組中從偏移量off開始的len個字節寫入輸入流。
4、flush()
功能:刷新此輸入流並強制寫出所有緩沖的輸出字節數。
5、close()
功能:關閉輸出流,釋放這個流的相關資源。
①字節數組輸入流:
1 package com.iotest; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.IOException; 5 public class ByteArryInputStreamDemo { 6 public static void main(String[] args) throws IOException { 7 String str = "abcdefghijk"; 8 byte[] strBuf = str.getBytes(); //字符串轉換成字節數組 9 ByteArrayInputStream bais = new ByteArrayInputStream(strBuf); 10 int data = bais.read(); //從字節數組輸入流讀取字節 11 while(data!=-1){ 12 char upper = Character.toUpperCase((char)data); 13 System.out.print(upper+" "); 14 data = bais.read(); 15 } 16 bais.close(); 17 } 18 }
程序運行結果:A B C D E F G H I J K
②字節數組輸出流:
1 package com.iotest; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 6 public class ByteArrayOutputStreamDemo { 7 public static void main(String[] args) throws IOException { 8 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 9 String s = "welcome to use ByteArrayOutputStreamDemo"; 10 byte[] buf = s.getBytes(); 11 baos.write(buf); //將指定的byte數組寫到字節數組輸出流中 12 System.out.println(baos.toString()); //將字節數組輸出流內容轉換成字符串輸出 13 //將字節數組輸出流中的內容復制到字節數組中 14 byte[] b = baos.toByteArray(); 15 for (int i = 0; i < b.length; i++) { 16 System.out.print((char)b[i]); 17 } 18 baos.close(); 19 } 20 }
程序運行結果:
welcome to use ByteArrayOutputStreamDemo
welcome to use ByteArrayOutputStreamDemo
③文件輸入輸出流的使用
1 package com.iotest; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 //復制圖片 9 public class FileInputStreamDemo { 10 public static void main(String[] args) throws IOException { 11 File file = new File("F:\\shar\\test\\logo17.gif"); 12 FileInputStream fis = new FileInputStream(file); //創建一個輸入流 13 //創建一個輸出流,后面一個參數true表示追加,原有內容不會被清除,默認為false 14 FileOutputStream fos = new FileOutputStream("F:\\shar\\test\\logo18.gif",false); 15 int ch = 0; 16 //方式一 17 /*while((ch=fis.read()) != -1){ 18 fos.write(ch); 19 }*/ 20 //方式二 21 /*byte[] b = new byte[1024]; 22 while((ch=fis.read(b)) != -1){ 23 fos.write(b,0,ch); 24 }*/ 25 //方式三 26 byte[] b = new byte[fis.available()]; 27 fis.read(b); //首先把fis的內容讀到字節數組b里面 28 fos.write(b);//再把字節數組b的內容通過輸出流寫到指定文件 29 //關閉流 30 fos.close(); 31 fis.close(); 32 } 33 34 }
④管道流的使用:
一個PipedInputStream對象必須和一個PipedOutputStream對象進行連接從而產生一個通信管道。通常一個線程從管道輸出流寫入數據,另一個線程從管道輸入流中讀取數據。當線程A執行管道輸入流的read()方法時,如果暫時沒有數據,這個線程就會被阻塞,只有當線程B想管道輸出流寫了數據后,線程A才會恢復運行。
package com.iotest; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; /* * 管道流 */ class Sender extends Thread{ private PipedOutputStream out = new PipedOutputStream(); public PipedOutputStream getOut() { return out; } @Override public void run() { String s = "hello world"; try { out.write(s.getBytes()); out.close(); } catch (Exception e) { // TODO: handle exception } } } public class Receiver extends Thread{ private PipedInputStream in; public Receiver(Sender sender) throws IOException { in = new PipedInputStream(sender.getOut()); } @Override public void run() { try { int data; while((data=in.read())!=-1){ System.out.print((char)data); } in.close(); } catch (Exception e) { // TODO: handle exception } } public static void main(String[] args) throws IOException { Sender sender = new Sender(); Receiver r = new Receiver(sender); sender.start(); r.start(); } }
⑤緩沖流的使用:
1 package com.iotest; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 10 public class TestPrime { 11 private BufferedInputStream bis = null; 12 private BufferedOutputStream bos = null; 13 String fileName = "F:\\shar\\test\\test2.txt"; 14 static int s,p; 15 //判斷是否是質數 16 public boolean isPrime(int n){ 17 for(int i=2;i<=n/2;i++){ 18 if(n%i == 0){ 19 return false; 20 } 21 } 22 return true; 23 } 24 void printPrime(int m) throws IOException{ 25 //將字節流轉緩沖流 26 bos = new BufferedOutputStream(new FileOutputStream(fileName)); 27 int j = 0; 28 for (int i = 2; i < m; i++) { 29 if(isPrime(i)){ 30 j++; 31 if(j%s == 0){ 32 String s = String.valueOf(i)+" "; 33 bos.write(s.getBytes()); 34 bos.write("\r\n".getBytes()); 35 }else{ 36 String s = String.valueOf(i)+" "; 37 bos.write(s.getBytes()); 38 } 39 } 40 } 41 bos.flush(); 42 bos.close(); 43 } 44 void getPrime() throws IOException{ 45 //將字節流轉緩沖流 46 bis = new BufferedInputStream(new FileInputStream(fileName)); 47 int c = bis.read(); 48 while(c != -1){ 49 char ch = (char)c; 50 System.out.print(ch); 51 c = bis.read(); 52 } 53 } 54 /** 55 * @param args 56 * @throws IOException 57 */ 58 public static void main(String[] args) throws IOException { 59 TestPrime t = new TestPrime(); 60 p = 100; 61 s = 10; 62 t.printPrime(p); 63 t.getPrime(); 64 } 65 66 }
如果不用緩沖流的話,程序是讀一個數據,寫一個數據。這樣在數據量大的程序中非常影響效率。 緩沖流作用是把數據先寫入緩沖區,等緩沖區滿了,再把數據寫到文件里。這樣效率就大大提高了。