1 package test03; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class Streamtest { 10 public static void main(String[] args) throws IOException { 11 long start = System.currentTimeMillis(); 12 //test1();//2421 13 //test2();//8 14 // test3();//52 15 test4();//6 16 System.out.println(System.currentTimeMillis()-start); 17 } 18 //基本字節流,一次讀寫一個字節 19 public static void test1() throws IOException { 20 //封裝源文件 21 FileInputStream fis = new FileInputStream("G:/Lighthouse.jpg"); 22 //封裝目標文件 23 FileOutputStream fos = new FileOutputStream("G:/demo/lighthouse01.jpg"); 24 //讀寫 25 int b = 0; 26 while((b = fis.read())!=-1) { 27 fos.write(b); 28 } 29 //關閉資源 30 fis.close(); 31 fos.close(); 32 } 33 //基本字節流 一次一個字節數組 34 public static void test2() throws IOException { 35 FileInputStream fis = new FileInputStream("G:/Lighthouse.jpg"); 36 FileOutputStream fos = new FileOutputStream("G:/demo/lighthouse02.jpg"); 37 38 //讀寫 39 int len =0; 40 byte[] bys = new byte[1024]; 41 while((len = fis.read(bys))!=-1) { 42 fos.write(bys); 43 } 44 //關閉資源 45 fis.close(); 46 fos.close(); 47 } 48 //緩衝字節流 一次一個字節 49 public static void test3() throws IOException { 50 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:/Lighthouse.jpg")); 51 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:/demo/lighthouse03.jpg")); 52 //讀寫 53 int b = 0; 54 while((b = bis.read())!=-1) { 55 bos.write(b); 56 } 57 bis.close(); 58 bos.close(); 59 60 } 61 //緩衝字節流 一次一個字節數組 62 public static void test4() throws IOException { 63 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:/Lighthouse.jpg")); 64 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:/demo/lighthouse04.jpg")); 65 //讀寫 66 int len = 0; 67 byte[] bys = new byte [1024]; 68 while((len = bis.read(bys))!=-1) { 69 bos.write(bys); 70 } 71 bis.close(); 72 bos.close(); 73 74 75 76 } 77 78 }