Java的IO流操作

《《《《《《Java的IO流操作》》》》》》》

package IO流;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.text.SimpleDateFormat;
 import java.util.Date;

 public class ioText {
             /*
              *@auther 何桂坤 2010-8-26
              */
  public static void main(String[] args) {
   try{
    //獲取存在的文件
    File file = new File("f://心事小寫/生日小寫.txt");//獲得文本文件生日小寫.txt
    FileInputStream inputStream = new FileInputStream(file);//實例化輸入流
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//創建讀取文件流
    BufferedReader br = new BufferedReader(inputStreamReader);//讀取文件流
    //---------------------
    //實例化空文本文件
               Date time=new Date();//間
               SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
               String t=format.format(time);//在文件名前加上時間
    FileOutputStream outputStream = new FileOutputStream("f://IO流測試/"+t+file.getName());//用原來的 文件名作爲新的文件名
    OutputStreamWriter owp = new OutputStreamWriter(outputStream);//創建寫出流
    //------------------owp-----
    String s = null;
    while((s=br.readLine())!=null){
     System.out.println(s);
     owp.write("/r/n"+s);///r/n爲換行 
     }
    SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    owp.write("/r/n/r/n"+format2.format(time)+"執筆而寫");
    owp.flush();
    owp.close();
    br.close();
    inputStreamReader.close();
   }catch(Exception e){
    e.printStackTrace();
   }
  }

 }

     //代碼來自heguikun個人網站

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章