浪跡天涯
          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文件操作
          主站蜘蛛池模板: 南开区| 微博| 介休市| 九江市| 太湖县| 梅河口市| 天全县| 五华县| 高安市| 荔浦县| 莱西市| 定兴县| 田阳县| 新干县| 古交市| 昆山市| 元谋县| 康平县| 大荔县| 闻喜县| 廉江市| 肃南| 临海市| 全州县| 白银市| 黑河市| 通江县| 惠水县| 安西县| 娱乐| 新巴尔虎左旗| 广安市| 阳山县| 贵阳市| 新龙县| 辉县市| 高淳县| 抚宁县| 秦安县| 莆田市| 台湾省|