asp.net+Android+IOS開發 、Net培訓、期待與您交流!
(前言:本篇文章主要依據畢向東老師的課程視頻整理而成,如要詳細學習,請觀看畢老師視頻 百度網盤鏈接地址:http://pan.baidu.com/s/1mgrkJWW)
目錄:1、字節流 基類InputStream/OutputStream 2、字符流 基類Reader/Writer
注意:由這四類派生出來的子類名稱都是以其父類作為子類名的后綴。
1、字節流 基類InputStream/OutputStream
下圖為字節流類體系結構圖,紅色部分為常用部分
舉幾個典型例子來更好的了解如何應用
三種以字節流形式讀取指定文件輸出到控制臺的方法(為簡便起見,都被封裝于各個函數中)
1 //讀取字節流,單個字符為單位進行 2 public static void readStream(String filename){ 3 4 FileInputStream fis=null; 5 try { 6 fis=new FileInputStream(filename); 7 //以read方法的還回值為條件,判斷是否讀取結束,遇到非char型的就雞雞了 8 int cha; 9 while((cha=fis.read())!=-1){10 System.out.PRintln((char)cha);11 }12 System.out.println("逐字符方式讀取文件內容已結束...");13 } catch (FileNotFoundException e) {14 e.printStackTrace();15 } catch (IOException e) {16 e.printStackTrace();17 }finally{try {18 if(fis!=null) 19 fis.close();//讀取結束,判斷若流已被創建,則關閉流資源20 } catch (IOException e) {21 e.printStackTrace();22 }23 }24 }25 //讀取字節流,字符數組為單位進行,比較好用26 public static void readStream1(String filename){27 28 FileInputStream fis=null;29 try {30 fis=new FileInputStream(filename);31 //定義每次讀取的字符數組大小32 byte[] cha=new byte[1024];33 int len=0;34 while((len=fis.read(cha))!=-1){35 System.out.println(new String(cha,0,len));36 }37 System.out.println("逐字符數組讀取結束...");38 } catch (FileNotFoundException e) {39 e.printStackTrace();40 } catch (IOException e) {41 e.printStackTrace();42 }finally{try {43 if(fis!=null) 44 fis.close();//讀取結束,判斷若流已被創建,則關閉流資源45 } catch (IOException e) {46 e.printStackTrace();47 }48 }49 }50 51 //慎用,讀取文件太大會出問題。52 public static void readStream2(String filename){53 54 FileInputStream fis=null;55 try {56 fis=new FileInputStream(filename);57 //定義每次讀取的字符數組的大小為整個文件全部58 byte[] cha=new byte[fis.available()];59 fis.read(cha);60 System.out.println(new String(cha));61 System.out.println("整篇一次性讀取結束...");62 63 } catch (FileNotFoundException e) {64 e.printStackTrace();65 } catch (IOException e) {66 e.printStackTrace();67 }finally{try {68 if(fis!=null)69 fis.close();70 } catch (IOException e) {71 e.printStackTrace();72 }73 } 74 }
2、字符流 基類Reader/Writer
下圖為字節流類體系結構圖,紅色部分為常用部分
三種以字符流方式讀取數據的辦法
1 //逐字符的原始讀取文件中字符,讀取的結果打印到控制臺 2 public static void readChar(String filename){ 3 4 FileReader fr=null; 5 try { 6 fr=new FileReader(filename); 7 //以read函數返回結果來判斷是否讀完,進行循環 8 int cha; 9 while((cha=fr.read())!=-1){10 System.out.print((char)cha);11 }12 } catch (FileNotFoundException e) {13 e.printStackTrace();14 } catch (IOException e) {15 e.printStackTrace();16 }finally{17 try {18 if(fr!=null) 19 fr.close();//先判斷流資源是否存在,然后關閉流資源20 } catch (IOException e) {21 e.printStackTrace();22 }23 }24 }25 //以字符串數組為單位讀取數據,結果打印到控制臺26 public static void readChars(String filename){27 28 FileReader fr=null;29 try {30 fr=new FileReader(filename);31 //定義字符數組的大小32 char[] chars=new char[1024];33 int number; 34 while((number=fr.read(chars))!=-1){35 //將字符數組轉換成字符串,打印結果36 System.out.println(new String(chars,0,number));37 } 38 } catch (FileNotFoundException e) {39 e.printStackTrace();40 } catch (IOException e) {41 e.printStackTrace();42 }finally{43 try {44 if(fr!=null) 45 fr.close();//先判斷流資源是否存在,然后關閉流資源46 } catch (IOException e) {47 e.printStackTrace();48 }49 }50 }51 //字符操作,以字符串數組為單位讀取數據,結果打印到控制臺并且將行數打印出。增加了一個緩沖區,提高讀取效率。52 public static void bufferReader(String filename){53 54 LineNumberReader buff=null;55 try {56 buff=new LineNumberReader(new FileReader(filename)); 57 buff.setLineNumber(0);//指定行號58 59 String line;60 while((line=buff.readLine())!=null){61 System.out.println(buff.getLineNumber()+":"+line);//打印帶行號的內容62 }63 } catch (FileNotFoundException e) {64 e.printStackTrace();65 } catch (IOException e) {66 e.printStackTrace();67 }finally{68 try {69 if(buff!=null)70 buff.close();//先判斷流資源是否存在,然后關閉流資源71 } catch (IOException e) {72 // FIXME Auto-generated catch block73 e.printStackTrace();74 }75 }76 }
兩種方式從控制臺讀取字符存入自定文件中
1 //字符操作,讀取控制臺錄入,讀到kill行結束操作,保存到filename.txt文件中,以行為單位操作 2 public static void write(String filename){ 3 //讀取控制臺錄入 4 System.out.println("請錄入(kill為結束命令,文件保存位置-"+filename+"):"); 5 BufferedReader buff=null; 6 FileWriter fw=null; 7 try { 8 buff = new BufferedReader(new InputStreamReader(System.in)); 9 fw = new FileWriter(filename,true);10 //只要讀的行數據不為空就循環向文件中寫數據11 String line;12 while((line=buff.readLine())!=null){13 if(line.equals("kill")) 14 break; 15 fw.write(line); 16 }17 System.out.println("錄入已經結束...");//錄入結束提示18 } catch (IOException e) {19 e.printStackTrace();20 }finally{21 try {22 if(fw!=null) 23 fw.close();//關閉流資源24 } catch (IOException e) {25 e.printStackTrace();26 }27 try {28 if(buff!=null) 29 buff.close();//關閉流資源30 } catch (IOException e) {31 e.printStackTrace();32 } 33 }34 }35 //字符操作,讀取控制臺錄入,保存到filename.txt文件中,加了一個緩沖區,提高存儲效率36 public static void bufferWriter(String filename){37 38 BufferedWriter buffwr=null;39 BufferedReader buffre=null;40 try {41 FileWriter fw=new FileWriter(filename,true);42 buffre=new BufferedReader(new InputStreamReader(System.in)); 43 buffwr=new BufferedWriter(fw);44 System.out.println("請錄入(kill為結束命令,文件保存位置-"+filename+"):");45 46 String line;47 while((line=buffre.readLine())!=null){48 if(line.equals("kill")) 49 break;50 buffwr.write(line);51 buffwr.newLine();//將換行符讀進保存52 }53 System.out.println("錄入已經結束...");54 55 } catch (IOException e) {56 e.printStackTrace();57 }finally{ 58 try {59 if(buffwr!=null)60 buffwr.close();61 } catch (IOException e) {62 e.printStackTrace();63 }64 try {65 if(buffre!=null)66 buffre.close();67 } catch (IOException e) {68 e.printStackTrace();69 }70 }71 }
兩種文本文件拷貝方式
1 //復制文本文件,從from到to 2 public static void copyTxt(String from,String to){ 3 //定義讀寫文件 4 FileReader fr=null; 5 FileWriter fw=null; 6 7 try { 8 //創建讀寫文件對象 9 fr=new FileReader(from);10 fw=new FileWriter(to,true);11 //定義數組大小12 char[] buff=new char[1024];13 int num;14 while((num=fr.read(buff))!=-1){15 fw.write(buff, 0, num);16 }17 System.out.println("文件已經從 "+from+" 文件夾復制到了 "+to+" 文件夾...");18 } catch (IOException e) {19 e.printStackTrace();20 }finally{21 try {22 if(fr!=null) 23 fr.close();24 }25 catch (IOException e) {26 e.printStackTrace();27 }28 try{29 if(fw!=null) 30 fw.close();31 }32 catch (IOException e) {33 e.printStackTrace();34 }35 }36 }37 //用裝飾類保證一下,提高效率38 public static void bufferCopyTxt(String from,String to){39 40 BufferedReader br=null;41 BufferedWriter bw=null; 42 try { 43 br=new BufferedReader(new FileReader(from));44 bw=new BufferedWriter(new FileWriter(to));45 46 String line;47 while((line=br.readLine())!=null){48 bw.write(line);49 bw.flush();50 }51 System.out.println("文件已經從 "+from+" 文件夾復制到了 "+to+" 文件夾..."); 52 } catch (IOException e) {53 e.printStackTrace();54 }finally{ 55 try {56 if(br!=null)57 br.close();58 } catch (IOException e) {59 // FIXME Auto-generated catch block60 e.printStackTrace();61 }62 try {63 if(bw!=null)64 br.close();65 } catch (IOException e) {66 // FIXME Auto-generated catch block67 e.printStackTrace();68 }69 }70 }
復制圖片的操作,圖片必須以字節流的方式操作
1 //復制圖片,從from文件到to文件。用到字節流 2 public static void copyImag(String from,String to){ 3 //定義輸入輸出字節流 4 FileInputStream fis=null; 5 FileOutputStream fos=null; 6 7 try { 8 fis=new FileInputStream(from); 9 fos=new FileOutputStream(to);10 //定義每次復制的單位字符數組大小11 byte[] cha=new byte[1024];12 int len=0;13 while((len=fis.read(cha))!=-1){14 fos.write(cha,0,len);15 }16 } catch (FileNotFoundException e) {17 e.printStackTrace();18 } catch (IOException e) {19 e.printStackTrace();20 }finally{21 try {if(fis!=null)22 fis.close();23 } catch (IOException e) {24 // FIXME Auto-generated catch block25 e.printStackTrace();26 }27 try {if(fis!=null)28 fis.close();29 } catch (IOException e) {30 // FIXME Auto-generated catch block31 e.printStackTrace();32 }33 }34 }
初學者難免錯誤,歡迎評判指教,持續更正ing...........
ASP.Net+Android+IOS開發 、Net培訓、期待與您交流!
新聞熱點
疑難解答