java之IO流(位元組流)
一.位元組輸出流OutputStream
OutputStream此抽象類,是表示輸出位元組流的所有類的超類。操作的資料都是位元組,定義了輸出位元組流的基本共性功能方法。
輸出流中定義都是寫write方法:
close():關閉此輸出流並釋放與此流有關的所有系統資源
flush():重新整理此輸出流並強制寫出所有緩衝的輸出位元組
write(byte[] b):將b.length個位元組從指定的byte陣列寫入此輸出流
write(byte[] b,int off,int len):將指定byte陣列中從偏移量off開始的len個位元組寫入此輸出流。
write(int b):將指定的位元組寫入此輸出流。
1.FileOutputStream類
OutputStream有很多子類,其中子類FileOutputStream可用來寫入資料到檔案。FileOutputStream類,即檔案輸出流,是用於將資料寫入 File的輸出流。
FileOutputStream(File file):建立一個向指定File物件表示的檔案中寫入資料的檔案輸出流
FileOutputStream(String name):建立一個向具有指定名稱的檔案中寫入資料的輸出檔案流
2.FileOutputStream類寫入資料到檔案中
publicclass FileOutputStreamDemo { public static void main(String[] args) throws IOException { //需求:將資料寫入到檔案中。 //建立儲存資料的檔案。 File file = new File("c:\\file.txt"); //建立一個用於操作檔案的位元組輸出流物件。一建立就必須明確資料儲存目的地。 //輸出流目的是檔案,會自動建立。如果檔案存在,則覆蓋。 FileOutputStream fos = new FileOutputStream(file);//呼叫父類中的write方法。 byte[] data = "abcde".getBytes(); fos.write(data); //關閉流資源。 fos.close(); } }
3.給檔案中續寫和換行
FileOutputStream(File file,boolean append):建立一個向指定File物件表示的檔案中寫入資料的檔案輸出流。
FileOutputStream(String name,boolean append):建立一個向指定File物件表示的檔案中寫入資料的檔案輸出流。
public class FileOutputStreamDemo2 { public static void main(String[] args) throws Exception { File file = new File("c:\\file.txt"); FileOutputStream fos = new FileOutputStream(file, true); String str = "\r\n"+"aaa"; fos.write(str.getBytes()); fos.close(); } }
4.IO異常的處理
public class FileOutputStreamDemo3 { public static void main(String[] args) { File file = new File("c:\\file.txt"); //定義FileOutputStream的引用 FileOutputStream fos = null; try { //建立FileOutputStream物件 fos = new FileOutputStream(file); //寫出資料 fos.write("abcde".getBytes()); } catch (IOException e) { System.out.println(e.toString() + "----"); throw new RuntimeException("檔案寫入失敗,重試"); } finally { //判斷fos是否為null,不為null時,關閉資源 if (fos != null) { try { fos.close(); } catch (IOException e) { throw new RuntimeException("關閉資源失敗"); } } } } }
二.位元組輸入流InputStream
通過InputStream可以實現把記憶體中的資料讀到記憶體中
read():從輸入流中讀取資料的下一個位元組
read(byte[] b):從輸入流中讀取一定數量的位元組,並將其儲存在緩衝區陣列b中
1. FileInputStream類
FileInputStream(File file):通過開啟一個到實際檔案的連線來建立一個 FileInputStream,該檔案通過檔案系統中的File物件file指定
FileInputStream(String name):通過開啟一個到實際檔案的連線來建立一個 FileInputStream,該檔案通過檔案系統中的路徑名name指定
2.FileInputStream類讀取資料read方法
read():從輸入流中讀取資料的下一個位元組
public class FileInputStreamDemo { public static void main(String[] args) throws IOException { File file = new File("c:\\file.txt"); //建立一個位元組輸入流物件,必須明確資料來源,其實就是建立位元組讀取流和資料來源相關聯。 FileInputStream fis = new FileInputStream(file); //讀取資料。使用 read();一次讀一個位元組。 int ch = 0; while((ch=fis.read())!=-1){ System.out.println("ch="+(char)ch); } // 關閉資源。 fis.close(); } }
3.讀取資料read(byte[])方法
read(byte[] b):從輸入流中讀取一定數量的位元組,並將其儲存在緩衝區陣列b中。
public class FileInputStreamDemo2 { public static void main(String[] args) throws IOException { /* * 演示第二個讀取方法, read(byte[]); */ File file = new File("c:\\file.txt"); // 建立一個位元組輸入流物件,必須明確資料來源,其實就是建立位元組讀取流和資料來源相關聯。 FileInputStream fis = new FileInputStream(file); //建立一個位元組陣列。 byte[] buf = new byte[1024];//長度可以定義成1024的整數倍。 int len = 0; while((len=fis.read(buf))!=-1){ System.out.println(new String(buf,0,len)); } fis.close(); } }
三.複製檔案
1.複製檔案
public class CopyFileTest { public static void main(String[] args) throws IOException { //1,明確源和目的。 File srcFile = new File("c:\\YesDir\test.JPG"); File destFile = new File("copyTest.JPG"); //2,明確位元組流 輸入流和源相關聯,輸出流和目的關聯。 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); //3, 使用輸入流的讀取方法讀取位元組,並將位元組寫入到目的中。 int ch = 0; while((ch=fis.read())!=-1){ fos.write(ch); } //4,關閉資源。 fos.close(); fis.close(); } }
2.緩衝陣列複製檔案
public class CopyFileByBufferTest { public static void main(String[] args) throws IOException { File srcFile = new File("c:\\YesDir\test.JPG"); File destFile = new File("copyTest.JPG"); // 明確位元組流 輸入流和源相關聯,輸出流和目的關聯。 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); //定義一個緩衝區。 byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len);// 將陣列中的指定長度的資料寫入到輸出流中。 } // 關閉資源。 fos.close(); fis.close(); } }