新博客地址:Temijava
第一講:IO流(概述)linux
一,IO流的概述:git
二,IO流經常使用基類:github
=========注:由這四個基類派生出的子類名稱都是以父類名稱做爲後綴的,例如FileInputStream 是 InputStream 類的子類 FileWriter 是Writer 類的子類。前綴名是流對象的功能=========web
第二講:IO流(FileWriter)api
一,Writer類的基本瞭解(查閱API):數組
二,寫入字符流的步驟:app
===知識點擴展:Java語言自己是不能往硬盤裏面寫數據的(不一樣系統寫入數據方式不一樣)。Java的數據寫入會調用系統的寫入資源。系統資源使用後必須關閉===函數
三,代碼練習:編碼
1 //導入須要用到的包。 2 import java.io.*; 3 4 public class FileWriterDemo{ 5 public static void main(String args[]) throws IOException{ 6 7 8 //建立一個 FileWriter 對象 9 FileWriter fw=new FileWriter("Demo.txt"); 10 11 //調用Writer 方法寫入數據 12 fw.writer("HelloWorld"); 13 14 //刷新流 15 fw.flush(); 16 17 //關閉流,一樣會觸發刷新 18 fw.close(); 19 } 20 }
第三講:IO流(IO異常處理方式)
一,對於 IO 異常處理的一些說明:
二,代碼練習:
1 import java.io.*; 2 3 public class FileWriterDemo2{ 4 public static void main(String args[]){ 5 6 //在 try 語句外面創建對象引用 7 FileWriter fw=null; 8 9 10 //try 語句捕獲異常 11 try{ 12 13 14 //try 內部進行對象初始化 15 fw=new FileWriter("Demo.txt"); 16 17 //寫入數據 18 fw.write("HelloHeiMa"); 19 }catch (IOException e){ 20 System.out.println(e.toString()); 21 }finally{ 22 23 24 //關閉動做一樣進行 try 處理 25 try{ 26 //判斷 fw 是否爲空 27 if(fw!=null) 28 fw.close(); 29 }catch (IOException e){ 30 System.out.println(e.toSring()); 31 } 32 } 33 }
第四講:IO流(文件的續寫)
一,進行數據續寫思路的分析:
二,知識點擴充:
三,代碼練習:
1 import java.io.*; 2 3 4 public class FileWriterDemo3 { 5 public static void main(String args[]){ 6 7 //聲明FileWriter對象 8 FileWriter fw=null; 9 try{ 10 11 //以追加方式實例化FileWriter對象, 12 fw=new FileWriter("E:/demo.txt",true); 13 14 //向文件中追加數據 15 fw.write("abcd\r\nHeiMa"); 16 }catch(IOException e){ 17 System.out.println("內容寫入失敗"); 18 }finally{ 19 20 21 //關閉系統資源 22 try{ 23 if(fw!=null) 24 fw.close(); 25 }catch(IOException e){ 26 System.out.println("關閉流操做失敗"); 27 } 28 } 29 } 30 }
第五講:IO流(文本文件讀取方式一)
一,FileReader類的瞭解:
(File file) throws FileNotFoundException ===當指定的文件不存在時拋出FileNotFoundException====
public FileReader(File file) throws FileNotFoundException
二,文本文件讀取過程:
三,代碼練習:
1 import java.io.*; 2 3 4 public class FileWriterDemo4 { 5 public static void main(String args[]){ 6 7 8 //聲明文件讀寫對象 9 FileReader fr=null; 10 11 12 //實例化對象,進行文件讀寫 13 try{ 14 15 //若文件不存在拋出FileNotFoundException異常 16 fr=new FileReader("E:\\demo.txt"); 17 18 //定義int 變量ch 接受讀取結果 19 int ch; 20 21 //若讀取結果爲-1則表示到達文件末尾,讀取結束 22 while((ch=fr.read())!=-1) 23 System.out.println("ch"+(char)ch); 24 System.out.println("讀取完畢"); 25 26 27 //文件不存在的異常處理 28 }catch(FileNotFoundException e){ 29 System.out.println("指定的文件不存在"); 30 }catch(IOException e){ 31 System.out.println("讀取文件發生錯誤"); 32 }finally{ 33 34 //關閉系統資源 35 try{ 36 if(fr!=null) 37 fr.close(); 38 }catch(IOException e){ 39 System.out.println("關閉文件發生錯誤"); 40 } 41 } 42 } 43 }
第六講:IO流(文本文件讀取方式二)
一,用到的方法:
二,注意事項:
三,代碼練習:
1 import java.io.*; 2 3 4 public class FileWriterDemo4 { 5 public static void main(String args[]){ 6 7 8 //聲明文件讀寫對象 9 FileReader fr=null; 10 11 12 //實例化對象,進行文件讀寫 13 try{ 14 15 //若文件不存在拋出FileNotFoundException異常 16 fr=new FileReader("E:\\demo.txt"); 17 18 //定義整形變量表示讀取到的字符數量 19 int ch; 20 21 22 //定義字符數組向裏讀取字符 23 char[] cbuf=new char[1024]; 24 25 //若讀取結果爲-1則表示到達文件末尾,讀取結束 26 while((ch=fr.read(cbuf))!=-1) 27 28 //截取有效的字符數 29 System.out.println(new String(cbuf,0,ch)); 30 System.out.println("讀取完畢"); 31 32 33 //文件不存在的異常處理 34 }catch(FileNotFoundException e){ 35 System.out.println("指定的文件不存在"); 36 }catch(IOException e){ 37 System.out.println("讀取文件發生錯誤"); 38 }finally{ 39 40 //關閉系統資源 41 try{ 42 if(fr!=null) 43 fr.close(); 44 }catch(IOException e){ 45 System.out.println("關閉文件發生錯誤"); 46 } 47 } 48 } 49 }
第七講:IO流(文本文件讀取練習)
一,練習要求:
二,實現代碼:
1,打印文件:
package com.examp.ch18; import java.io.*; public class FileWriterDemo4 { public static void main(String args[]){ //聲明文件讀寫對象 FileReader fr=null; String filename="E:\\test.java"; //判斷文件名是否爲.java 結尾。 if(!filename.endsWith(".java")){ System.out.println("目標文件後墜名不是.java"); return; } //實例化對象,進行文件讀寫 try{ //若文件不存在拋出FileNotFoundException異常 fr=new FileReader(filename); //定義整形變量表示讀取到的字符數量 int ch; //定義字符數組向裏讀取字符 char[] cbuf=new char[1024]; //若讀取結果爲-1則表示到達文件末尾,讀取結束 while((ch=fr.read(cbuf))!=-1) //截取有效的字符數 System.out.println(new String(cbuf,0,ch)); System.out.println("讀取完畢"); //文件不存在的異常處理 }catch(FileNotFoundException e){ System.out.println("指定的文件不存在"); }catch(IOException e){ System.out.println("讀取文件發生錯誤"); }finally{ //關閉系統資源 try{ if(fr!=null) fr.close(); }catch(IOException e){ System.out.println("關閉文件發生錯誤"); } } } }
2,複製文件:
1 import java.io.*; 2 3 4 public class FileWriterDemo4 { 5 public static void main(String args[]){ 6 if(copy("E:\\test.java","F:\\demo.txt")) 7 8 9 //複製成功給出提示 10 System.out.println("複製成功"); 11 } 12 13 14 public static boolean copy(String srcfile,String dest){ 15 16 //聲明讀文件對象以及寫文件對象的引用。 17 FileReader fr=null; 18 FileWriter fw=null; 19 20 21 //定義int變量表示讀取的字符數 22 int ch; 23 24 //定義字符數組,用來讀取 25 char[] cbuf=new char[1024]; 26 try{ 27 28 //分別實例化源文件的讀對象,以及目的文件的寫對象 29 fr=new FileReader(srcfile); 30 fw=new FileWriter(dest); 31 32 33 //邊讀邊寫,實現文件複製 34 while((ch=fr.read(cbuf))!=-1){ 35 fw.write(cbuf, 0, ch); 36 } 37 38 39 //複製成功返回true 40 return true; 41 }catch(FileNotFoundException e){ 42 43 //產生異常,給出提示,返回false 44 System.out.println(e.toString()); 45 return false; 46 }catch(IOException e){ 47 48 49 //產生異常,給出提示,返回false 50 System.out.println("複製文件產生錯誤"); 51 return false; 52 }finally{ 53 54 //釋放系統資源 55 try{ 56 if(fr!=null) 57 fr.close(); 58 if(fw!=null) 59 fw.close(); 60 }catch(IOException e){ 61 62 63 //產生異常,給出提示。 System.out.println("關閉文件產生錯誤!"); 64 } 65 } 66 } 67 }