JAVA---IO流

來源:互聯網
上載者:User

標籤:次數   tput   文字   post   沒有   jpg   void   情況下   關閉   

分享下在java中,使用io查看檔案,編寫檔案,複製檔案。

 

1.複製圖片,未拋出異常

package com.newdream.class4;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class test_FileCopy {                 //未拋出異常     public static void main(String[] args) throws IOException {              //找到目標檔案              File inFile = new File("F:/3-150402141930.jpg");              File destFile = new File("E:/1.jpg");              //建立資料的輸入輸出通道              FileInputStream fileInputStream = new  FileInputStream(inFile);              FileOutputStream fileOutputStream = new FileOutputStream(destFile); //每新建立一個FileOutputStream的時候,預設情況下FileOutputStream 的指標是指向了檔案的開始的位置。 每寫出一次,指向都會出現相應移動。              //建立緩衝資料,邊讀邊寫              byte[] buf = new byte[1024];               int length = 0 ;               while((length = fileInputStream.read(buf))!=-1){                 fileOutputStream.write(buf,0,length); //寫出很多次資料,所以就必須要追加。              }              //關閉資源 原則: 先開後關,後開先關。              fileOutputStream.close();              fileInputStream.close();          }  }

 

2.查看檔案

 1 package com.newdream.class4; 2  3 import java.io.BufferedReader; 4 import java.io.FileReader; 5  6 public class CharIOReader {     7     //讀取檔案當中的內容     8     public static void main(String[] args) { 9         //讀取字元檔案10         //1.打通通道11         FileReader fr=null;  //2.建立字元流輸入緩衝區12         BufferedReader  br=null;13         try{        14              //把檔案內容放到緩衝區15              fr=new FileReader("E:\newdream.txt");16                       //3,從緩衝區讀取字元17              //從緩衝區讀取資料18              br=new BufferedReader(fr);             19              //使用while 讀出資料20              while(br.read()!=-1){21                  String date =br.readLine();22                  System.out.println(date);23              }             24         }catch (Exception e){25             System.out.println(e.toString());26         }finally{27             try{28                 br.close();    //5.關閉緩衝區,關閉通道29             }catch(Exception e){30                 System.out.println(e.toString());31             }            32         }33     }34 35 }

3.寫入檔案

 1 package com.newdream.class4; 2  3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileWriter; 7 import java.io.StringReader; 8  9 public class CharIOWriter {10     11     //將文字寫入指定檔案12     13 14     public static String data="我胡漢三又回來了QAQ!";15     public static void main(String[] args) {16         String line;//用來控制寫檔案結束17         BufferedWriter bw=null;18         BufferedReader br=null;19         try{20             FileWriter fr=new FileWriter("E:\newdream.txt");21             bw=new BufferedWriter(fr);22             //先讀取到字元流,再把字元流存到讀取流中23             br=new BufferedReader(new StringReader(data));24             //readLine() 為null 表示沒有內容了,結束25             while((line=br.readLine())!=null){26                 bw.write(line);27                 //寫入一行,重新整理一下,寫下一行28                 bw.flush();29             }30             System.out.println("寫入成功!");31         }catch(Exception e){32             System.out.println(e.toString());33         }finally{34             try{35                 //關閉緩衝區:先開啟後關閉,後開啟先關閉36                 br.close();37                 bw.close();38             }catch(Exception e){39                 System.out.println(e.toString());40             }41         }42     }43 }

4.複製電影

 1 package com.newdream.class4; 2  3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7  8 public class test_FileCopyMovie { 9     public static void main(String[] args) throws IOException {  10         //找到目標檔案  11         File inFile = new File("F:\\迅雷下載\\與君相戀100次.mp4");  12         File destFile = new File("D:\\100婚.mp4");  13         //建立資料的輸入輸出通道  14         FileInputStream fileInputStream = new  FileInputStream(inFile);  15         FileOutputStream fileOutputStream = new FileOutputStream(destFile); 16           17         //每新建立一個FileOutputStream的時候,預設情況下FileOutputStream 的指標是指向了檔案的開始的位置。 每寫出一次,指向都會出現相應移動。  18         //建立緩衝資料,邊讀邊寫  19         byte[] buf = new byte[1024];   20         int length = 0 ;   21         while((length = fileInputStream.read(buf))!=-1){22             fileOutputStream.write(buf,0,length); 23         }  24         //關閉資源 原則: 先開後關,後開先關。  25         fileOutputStream.close();  26         fileInputStream.close();  27     }28 29 }


--與上面複製檔案相同
本文僅代表作者觀點,系作者@請多指教 發表。歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。
http://www.cnblogs.com/yushengaqingzhijiao/p/8343319.html

 

JAVA---IO流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.