1 ////////////////////////////////////////////////////////////////
           2 //  
           3 //  程序:ReadWiteFile.java
           4 //
           5 //  描述:對文件讀寫操作
           6 //
           7 //  作者:JAVA-HE
           8 //
           9 //  時間:2006-12-20
          10 //
          11 //////////////////////////////////////////////////////////////
          12 
          13 package com.readwritefile;
          14 
          15 import java.io.*;
          16 public class ReadWiteFile
          17 {
          18     public ReadWiteFile ()
          19     {
          20     }
          21     public static void main(String args [])
          22     {
          23         ReadWiteFile managerFile = new ReadWiteFile();
          24         File readfile = new File("read.txt");
          25         File writefile = new File("write.txt");
          26         String content = "";
          27         try
          28         {
          29             content = managerFile.readFile(readfile);
          30             
          31         }catch(Exception ex)
          32         {
          33             System.out.println ("讀文件發生異常:"+ex.toString ());
          34         }
          35         try
          36         {
          37             managerFile.writeFile(writefile,content);
          38         }catch(Exception e)
          39         {
          40             System.out.println ("寫文件發生異常:"+e.toString ());
          41         }
          42     }
          43     private  String readFile (File readfile) throws Exception 
          44     {
          45         FileReader read = new FileReader(readfile);
          46         BufferedReader in = new BufferedReader(read);
          47         String readResult="";
          48         String str = "";
          49         while((str=in.readLine ())!= null)
          50         {
          51             String tem [] = str.split ("=");
          52             readResult += "m_objCitys.Add \""+tem[1]+"\", \""+tem[0]+"\""+"\n";
          53         }
          54         read.close ();
          55         in.close ();
          56         return readResult;
          57     }
          58 
          59     private void writeFile (File writefile, String content) throws Exception
          60     {
          61         FileWriter write = new FileWriter(writefile);
          62         BufferedWriter wr = new BufferedWriter(write);
          63         wr.write (content);
          64         wr.flush ();
          65         wr.close ();
          66         write.close ();
          67     }
          68     
          69 }
          70 
          71 
          72 
          73 
          74 

          posted on 2007-02-13 14:45 -274°C 閱讀(523) 評論(0)  編輯  收藏 所屬分類: JAVA

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916109
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 甘德县| 施甸县| 平邑县| 辽阳县| 潮州市| 沈丘县| 白水县| 宁陕县| 泾源县| 伊通| 汪清县| 于都县| 铁岭县| 永定县| 宝丰县| 山丹县| 射洪县| 林周县| 潢川县| 广丰县| 南皮县| 楚雄市| 天全县| 靖安县| 西乡县| 济南市| 保定市| 兴仁县| 柳河县| 华蓥市| 顺平县| 桓台县| 连州市| 贡嘎县| 萝北县| 南陵县| 兴化市| 高唐县| 金川县| 桃源县| 诸城市|