1、
1 package cn.kongxh.io3; 2 import java.io.File ; 3 import java.io.InputStream ; 4 import java.io.FileInputStream ; 5 public class InputStreamDemo01{ 6 public static void main(String args[]) throws Exception{ // 異常拋出,不處理 7 // 第1步、使用File類找到一個文件 8 File f= new File("d:" + File.separator + "test.txt") ; // 聲明File對象 9 // 第2步、通過子類實例化父類對象 10 InputStream input = null ; // 准備好一個輸入的對象 11 input = new FileInputStream(f) ; // 通過對象多態性,進行實例化 12 // 第3步、進行讀操作 13 byte b[] = new byte[1024] ; // 所有的內容都讀到此數組之中 14 input.read(b) ; // 讀取內容 網絡編程中 read 方法會阻塞 15 // 第4步、關閉輸出流 16 input.close() ; // 關閉輸出流 17 System.out.println("內容為:" + new String(b)) ; // 把byte數組變為字符串輸出 18 } 19 };
2、
1 package cn.kongxh.io3; 2 import java.io.File ; 3 import java.io.InputStream ; 4 import java.io.FileInputStream ; 5 public class InputStreamDemo02{ 6 public static void main(String args[]) throws Exception{ // 異常拋出,不處理 7 // 第1步、使用File類找到一個文件 8 File f= new File("d:" + File.separator + "test.txt") ; // 聲明File對象 9 // 第2步、通過子類實例化父類對象 10 InputStream input = null ; // 准備好一個輸入的對象 11 input = new FileInputStream(f) ; // 通過對象多態性,進行實例化 12 // 第3步、進行讀操作 13 byte b[] = new byte[1024] ; // 所有的內容都讀到此數組之中 14 int len = input.read(b) ; // 讀取內容 15 // 第4步、關閉輸出流 16 input.close() ; // 關閉輸出流\ 17 System.out.println("讀入數據的長度:" + len) ; 18 System.out.println("內容為:" + new String(b,0,len)) ; // 把byte數組變為字符串輸出 19 } 20 };
3、
1 package cn.kongxh.io3; 2 3 import java.io.File ; 4 import java.io.InputStream ; 5 import java.io.FileInputStream ; 6 public class InputStreamDemo03{ 7 public static void main(String args[]) throws Exception{ // 異常拋出,不處理 8 // 第1步、使用File類找到一個文件 9 File f= new File("d:" + File.separator + "test.txt") ; // 聲明File對象 10 // 第2步、通過子類實例化父類對象 11 InputStream input = null ; // 准備好一個輸入的對象 12 input = new FileInputStream(f) ; // 通過對象多態性,進行實例化 13 // 第3步、進行讀操作 14 byte b[] = new byte[(int)f.length()] ; // 數組大小由文件決定 15 int len = input.read(b) ; // 讀取內容 16 // 第4步、關閉輸出流 17 input.close() ; // 關閉輸出流\ 18 System.out.println("讀入數據的長度:" + len) ; 19 System.out.println("內容為:" + new String(b)) ; // 把byte數組變為字符串輸出 20 } 21 };
4、
1 package cn.kongxh.io3; 2 3 import java.io.File ; 4 import java.io.InputStream ; 5 import java.io.FileInputStream ; 6 public class InputStreamDemo04{ 7 public static void main(String args[]) throws Exception{ // 異常拋出,不處理 8 // 第1步、使用File類找到一個文件 9 File f= new File("d:" + File.separator + "test.txt") ; // 聲明File對象 10 // 第2步、通過子類實例化父類對象 11 InputStream input = null ; // 准備好一個輸入的對象 12 input = new FileInputStream(f) ; // 通過對象多態性,進行實例化 13 // 第3步、進行讀操作 14 byte b[] = new byte[(int)f.length()] ; // 數組大小由文件決定 15 for(int i=0;i<b.length;i++){ 16 b[i] = (byte)input.read() ; // 讀取內容 17 } 18 // 第4步、關閉輸出流 19 input.close() ; // 關閉輸出流\ 20 System.out.println("內容為:" + new String(b)) ; // 把byte數組變為字符串輸出 21 } 22 };
5、
1 package cn.kongxh.io3; 2 3 import java.io.File ; 4 import java.io.InputStream ; 5 import java.io.FileInputStream ; 6 public class InputStreamDemo05{ 7 public static void main(String args[]) throws Exception{ // 異常拋出,不處理 8 // 第1步、使用File類找到一個文件 9 File f= new File("d:" + File.separator + "test.txt") ; // 聲明File對象 10 // 第2步、通過子類實例化父類對象 11 InputStream input = null ; // 准備好一個輸入的對象 12 input = new FileInputStream(f) ; // 通過對象多態性,進行實例化 13 // 第3步、進行讀操作 14 byte b[] = new byte[1024] ; // 數組大小由文件決定 15 int len = 0 ; 16 int temp = 0 ; // 接收每一個讀取進來的數據 17 while((temp=input.read())!=-1){ 18 // 表示還有內容,文件沒有讀完 19 b[len] = (byte)temp ; 20 len++ ; 21 } 22 // 第4步、關閉輸出流 23 input.close() ; // 關閉輸出流\ 24 System.out.println("內容為:" + new String(b,0,len)) ; // 把byte數組變為字符串輸出 25 } 26 };
總結:InputStream操作的是字節、實現類分為幾個方向、內存、本地文件、網絡、其他線程管道