位元組輸入流
位元組輸入流流程
package com.cloud.day2; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Demo1 { public static void main(String[] args) throws IOException { readTest4(); } //讀取的方式一缺陷:無法讀取完整一個檔案的資料. public static void readTest1() throws IOException{ //1.找到目标檔案 File file = new File("F:\\a.txt"); //2.建立資料通道 FileInputStream fileInputStream = new FileInputStream(file); //3.讀取檔案的資料 int content = fileInputStream.read(); System.out.println("read content:"+(char)content); //4.關閉釋放資源 fileInputStream.close(); } //方式2 :使用循環讀取檔案的資料 public static void readTest2() throws IOException{ long startTime = System.currentTimeMillis(); File file = new File("F:\\dd.jpg"); FileInputStream fileInputStream = new FileInputStream(file); int content = 0; while((content = fileInputStream.read())!=-1){ System.out.print((char)content); } fileInputStream.close(); long endTime = System.currentTimeMillis(); System.out.println("讀取時間:"+(endTime-startTime)); } //方式3:使用緩沖數組讀取。 缺點:無法讀取完整一個檔案的資料。 public static void readTest3() throws IOException{ File file = new File("F:\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); byte []buf = new byte[1024]; // 如果使用read讀取資料傳入位元組數組,那麼資料是存儲到位元組數組中的, //而這時候read方法的傳回值是表示的是本次讀取了幾個位元組資料到位元組數組中。 int len = fileInputStream.read(buf); System.out.println("len="+len); //使用位元組數組建構字元串 String content = new String(buf, 0, len); System.out.println("content:"+content); fileInputStream.close(); } //方式4:使用緩沖數組配合循環一起讀取 public static void readTest4() throws IOException{ long startTime = System.currentTimeMillis(); File file = new File("F:\\dd.jpg"); FileInputStream fileInputStream = new FileInputStream(file); int len = 0; //存儲讀取到的資料 緩沖數組的長度一般是1024的倍數,因為與計算機的處理機關。 //理論上緩沖數組越大,效率越高 byte [] buf = new byte[1024]; while((len = fileInputStream.read())!=-1){ System.out.println(new String(buf,0,len)); } fileInputStream.close(); long endTime = System.currentTimeMillis(); System.out.println("讀取時間:"+(endTime-startTime)); } } |
流關閉問題
先打開的流後關閉
package com.cloud.day2; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Demo2 { public static void main(String[] args) throws IOException { //1.找到目标檔案 File file = new File("F:\\a.txt"); //2.建立資料輸入通道 FileInputStream fileInputStream = new FileInputStream(file); //3.緩沖位元組數組讀取檔案 byte []buf = new byte[1024]; int length = 0; while((length=fileInputStream.read(buf))!=-1){ System.out.println(new String(buf)); } //4.釋放資源檔案 fileInputStream.close(); } } |
拷貝圖檔
package com.cloud.day3; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo1 { public static void main(String[] args) throws IOException { File inFile = new File("F:\\dd.jpg"); File destFile = new File("E:\\dd.jpg"); FileInputStream fileInputStream = new FileInputStream(inFile); //每新建立一個FileOutputStream的時候,預設情況下FileOutputStream 的指針是指向了檔案的開始的位置。 //每寫出一次,指向都會出現相應移動。 FileOutputStream fileOutputStream = new FileOutputStream(destFile); //建立緩沖資料,邊讀邊寫 byte []buf = new byte[1024]; int length=0; while((length=fileInputStream.read())!=-1){ fileOutputStream.write(buf, 0, length); } //關閉原則,先開後關,後開先關 fileOutputStream.close(); fileInputStream.close(); } } |
位元組輸出流
位元組輸出流流程
package com.cloud.day3; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Demo2 { public static void main(String[] args) throws IOException { writeTest1(); } //使用位元組數組寫出資料 //每次隻能寫一個位元組的資料出去 public static void writeTest1() throws IOException{ File file = new File("F:\\a.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write('s'); fileOutputStream.write('p'); fileOutputStream.write('r'); fileOutputStream.write('i'); fileOutputStream.write('n'); fileOutputStream.write('g'); fileOutputStream.close(); } } |
寫資料的類型
package com.cloud.day3; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; public class Demo3 { public static void main(String[] args) throws IOException { readTest(); } public static void readTest() throws IOException{ File file = new File("F:\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); byte []buf=new byte[1024]; int length = fileInputStream.read(buf); System.out.println("位元組數組的内容是:"+Arrays.toString(buf)); fileInputStream.close(); } public static void writeTest()throws FileNotFoundException,IOException{ File file = new File("F:\\a.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(511); fileOutputStream.close(); } } |
IO流的異常處理
package com.cloud.day3; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo4 { public static void main(String[] args) { copyImage(); } //拷貝圖檔 public static void copyImage(){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { //找到目标檔案 File inFile = new File("F:\\dd.jpg"); File outFile = new File("F:\\d.jpg"); //建立流通道 fileInputStream = new FileInputStream(inFile); fileOutputStream = new FileOutputStream(outFile); //建立緩沖數組,邊讀邊寫 byte[] buf = new byte[1024]; int length = 0; while((length = fileInputStream.read(buf))!=-1){ fileOutputStream.write(buf, 0, length); } } catch (IOException e) { System.out.println("拷貝圖檔出錯..."); throw new RuntimeException(e); }finally{ try { if(fileOutputStream!=null){ fileOutputStream.close(); System.out.println("關閉輸出流對象成功..."); } } catch (IOException e) { System.out.println("關閉輸出流對象失敗..."); throw new RuntimeException(e); }finally{ if(fileInputStream!=null){ try { fileInputStream.close(); System.out.println("關閉輸入流成功..."); } catch (IOException e) { System.out.println("關閉輸入流失敗..."); throw new RuntimeException(e); } } } } } //讀取檔案測試 public static void readTest(){ FileInputStream fileInputStream = null; try { //找到目标檔案 File file = new File("F:\\a.txt"); //建立資料輸入通道 fileInputStream = new FileInputStream(file); //建立緩沖數組讀取資料 byte[] buf = new byte[1024]; int length = 0; while((length = fileInputStream.read(buf))!=-1){ System.out.println(new String(buf, 0, length)); } } catch (IOException e) { System.out.println("讀取資源檔案出錯..."); throw new RuntimeException(e); }finally{ try { if(fileInputStream!=null){ fileInputStream.close(); System.out.println("關閉資源檔案成功..."); } } catch (IOException e) { System.out.println("關閉資源檔案失敗..."); throw new RuntimeException(e); } } } } |
緩沖位元組流
緩沖位元組輸入流
package com.cloud.day3; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo5 { public static void main(String[] args) throws IOException { readTest(); } public static void readTest2() throws IOException{ File file = new File("F:\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedInputStream.read(); FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); fileOutputStream.write(null); //讀取檔案資料 int content = 0; while((content = fileInputStream.read())!=-1){ System.out.println((char)content); } //關閉資源,這裡實際上是關閉FileInputSream bufferedInputStream.close(); } public static void readTest() throws IOException{ //讀取檔案使用緩沖數組 File file = new File("F:\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); //建立緩沖數組讀取資料 byte[] buf = new byte[1024]; int length = 0; while((length = fileInputStream.read(buf))!=-1){ System.out.println(new String(buf, 0, length)); } fileInputStream.close(); } } |
緩沖位元組輸出流
package com.cloud.day3; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Demo6 { public static void main(String[] args) throws IOException { File file = new File("F:\\a.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); bufferedOutputStream.write("Hello,world".getBytes()); bufferedOutputStream.close(); } } |
緩沖位元組流拷貝圖檔
package com.cloud.day3; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo7 { public static void main(String[] args) throws IOException { //找到目标檔案 File inFile = new File("F:\\dd.jpg"); File outFile = new File("F:\\1.jpg"); //建立資料通道 FileInputStream fileInputStream = new FileInputStream(inFile); FileOutputStream fileOutputStream = new FileOutputStream(outFile); //建立緩沖流 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); //邊讀邊寫 int content = 0; while((content = bufferedInputStream.read())!=-1){ bufferedOutputStream.write(content); } bufferedInputStream.close(); bufferedOutputStream.close(); } } |