java--io位元組流和字元流
阿新 • • 發佈:2020-07-31
位元組流
位元組輸出流OutputStream
FileOutputStream類
public class 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(); } }
給檔案中續寫和換行
構造方法:
給檔案中續寫資料和換行,程式碼演示:
public class FileOutputStreamDemo2 { public static void main(String[] args) throwsException { File file = new File("c:\\file.txt"); FileOutputStream fos = new FileOutputStream(file, true); String str = "\r\n" +"aaa"; fos.write(str.getBytes()); fos.close(); } }
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
基本方法:
int read():讀取一個位元組並返回,沒有位元組返回-1.
int read(byte[]): 讀取一定量的位元組數,並存儲到位元組陣列中,返回讀取到的位元組數。
FileInputStream類
FileInputStream類讀取資料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(); } }
讀取資料read(byte[])方法
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(); } }
緩衝陣列方式複製檔案
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(); } }
字元流
位元組流讀取字元的問題
public class CharStreamDemo { public static void main(String[] args) throws IOException { //給檔案中寫中文 writeCNText(); //讀取檔案中的中文 readCNText(); } //讀取中文 public static void readCNText() throws IOException { FileInputStream fis = new FileInputStream("c:\\cn.txt"); int ch = 0; while((ch = fis.read())!=-1){ System.out.println(ch); } } //寫中文 public static void writeCNText() throws IOException { FileOutputStream fos = new FileOutputStream("c:\\cn.txt"); fos.write("歡迎你".getBytes()); fos.close(); } }
字元輸入流Reader
public class CharStreamDemo { public static void main(String[] args) throws IOException { //給檔案中寫中文 writeCNText(); //讀取檔案中的中文 readCNText(); } //讀取中文 public static void readCNText() throws IOException { FileReader fr = new FileReader("D:\\test\\cn.txt"); int ch = 0; while((ch = fr.read())!=-1){ //輸出的字元對應的編碼值 System.out.println(ch); //輸出字元本身 System.out.println((char)ch); } } //寫中文 public static void writeCNText() throws IOException { FileOutputStream fos = new FileOutputStream("D:\\test\\cn.txt"); fos.write("歡迎你".getBytes()); fos.close(); } }
字元輸出流Writer
public class FileWriterDemo { public static void main(String[] args) throws IOException { //演示FileWriter 用於操作檔案的便捷類。 FileWriter fw = new FileWriter("d:\\text\\fw.txt"); fw.write("你好謝謝再見");//這些文字都要先編碼。都寫入到了流的緩衝區中。 fw.flush(); fw.close(); } }
flush()和close()的區別?
flush():將流中的緩衝區緩衝的資料重新整理到目的地中,重新整理後,流還可以繼續使用。
close():關閉資源,但在關閉前會將緩衝區中的資料先重新整理到目的地,否則丟失資料,然後在關閉流。流不可以使用。如果寫入資料多,一定要一邊寫一邊重新整理,最後一次可以不重新整理,由close完成重新整理並關閉。
練習:複製文字檔案:
public class CopyTextFileTest { public static void main(String[] args) throws IOException { copyTextFile(); } public static void copyTextFile() throws IOException { //1,明確源和目的。 FileReader fr = new FileReader("c:\\cn.txt"); FileWriter fw = new FileWriter("c:\\copy.txt"); //2,為了提高效率。自定義緩衝區陣列。字元陣列。 char[] buf = new char[1024]; int len = 0; while((len=fr.read(buf))!=-1){ fw.write(buf,0,len); } /*2,迴圈讀寫操作。效率低。 int ch = 0; while((ch=fr.read())!=-1){ fw.write(ch); } */ //3,關閉資源。 fw.close(); fr.close(); } }