浪跡天涯
          web報表設計器....
          posts - 61,comments - 71,trackbacks - 0
          ?1 package ?com.paiao.bdpm.flow.base;
          ?2
          ?3 import ?java.io.BufferedReader;
          ?4 import ?java.io.BufferedWriter;
          ?5 import ?java.io.File;
          ?6 import ?java.io.FileReader;
          ?7 import ?java.io.FileWriter;
          ?8
          ?9 public ? class ?ReadTextFile? {
          10 ???? public ?BufferedReader?bufread;
          11 ???? public ?BufferedWriter?bufwriter;
          12 ????File?writefile;
          13 ????String?filepath,?filecontent,?read;
          14 ????String?readStr? = ? "" ;
          15 ???? // 從文本文件中讀取內容
          16 ???? public ?String?readfile(String?path)???
          17 ???? {
          18 ???????? try ? {
          19 ????????????filepath? = ?path;? // 得到文本文件的路徑
          20 ????????????File?file? = ? new ?File(filepath);
          21 ????????????FileReader?fileread? = ? new ?FileReader(file);
          22 ????????????bufread? = ? new ?BufferedReader(fileread);
          23 ???????????? while ?((read? = ?bufread.readLine())? != ? null )? {
          24 ????????????????read? = ?read + " \r\n " ;????????????
          25 ????????????????readStr? = ?readStr? + ?read;
          26 ????????????}

          27 ????????}
          ? catch ?(Exception?d)? {
          28 ????????????System.out.println(d.getMessage());
          29 ????????}

          30 ???????? return ?readStr;? // 返回從文本文件中讀取內容
          31 ????}

          32
          33 ???? // 向文本文件中寫入內容
          34 ???? public ? void ?writefile(String?path,?String?content,? boolean ?append)? {
          35 ???????? try ? {
          36 ???????????? boolean ?addStr? = ?append;? // 通過這個對象來判斷是否向文本文件中追加內容
          37 ????????????filepath? = ?path;? // 得到文本文件的路徑
          38 ????????????filecontent? = ?content;? // 需要寫入的內容
          39 ????????????writefile? = ? new ?File(filepath);
          40 ???????????? if ?(writefile.exists()? == ? false )? // 如果文本文件不存在則創建它?
          41 ???????????? {
          42 ????????????????writefile.createNewFile();
          43 ????????????????writefile? = ? new ?File(filepath);? // 重新實例化
          44 ????????????}

          45 ????????????FileWriter?filewriter? = ? new ?FileWriter(writefile,?addStr);
          46 ???????????? // 刪除原有文件的內容
          47 ????????????java.io.RandomAccessFile?file = ? new ?java.io.RandomAccessFile(path, " rw " );
          48 ????????????file.setLength( 0 );
          49 ???????????? // 寫入新的文件內容
          50 ????????????filewriter.write(filecontent);
          51 ????????????filewriter.close();
          52 ????????????filewriter.flush();
          53 ????????}
          ? catch ?(Exception?d)? {
          54 ????????????System.out.println(d.getMessage());
          55 ????????}

          56 ????}

          57
          58 ???? public ? static ? void ?main(String[]?args)? throws ?Exception? {
          59 ????????ReadTextFile?parse? = ? new ?ReadTextFile();
          60 ????????String?filecontent? = ?parse.readfile( " c:/applicationContext.xml " );????????
          61 ????????parse.writefile( " c:/applicationContext.xml " ,filecontent, true );
          62 ????????
          63 ????}

          64 }
          posted on 2007-08-15 15:10 JJCEA 閱讀(3198) 評論(0)  編輯  收藏 所屬分類: java文件操作
          主站蜘蛛池模板: 临潭县| 乐平市| 昭苏县| 姜堰市| 丰都县| 定日县| 威宁| 穆棱市| 宜君县| 松潘县| 新津县| 东海县| 南宫市| 定结县| 宜都市| 泰兴市| 洱源县| 青州市| 巴里| 四川省| 定南县| 山西省| 枣阳市| 攀枝花市| 将乐县| 桂东县| 佛坪县| 右玉县| 灌云县| 鱼台县| 大荔县| 同心县| 岳普湖县| 唐海县| 阿拉善盟| 定兴县| 陵川县| 吉木乃县| 尼玛县| 儋州市| 夏邑县|