學(xué)習(xí) 學(xué)習(xí) 再學(xué)習(xí)

          學(xué)習(xí)才是唯一的出路

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            31 隨筆 :: 1 文章 :: 8 評(píng)論 :: 0 Trackbacks
          import java.io.*;


          /**
           * 
           * 功能描述:創(chuàng)建TXT文件并進(jìn)行讀、寫(xiě)、修改操作
           *      
           * 
          @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
           * 
          @version 1.0 
           * Creation date: 2007-12-18 - 下午06:48:45
           
          */

          public class ReadWriteFile {
              
          public static BufferedReader bufread;
              
          //指定文件路徑和名稱
              private static String path = "D:/suncity.txt";
              
          private static  File filename = new File(path);
              
          private static String readStr ="";


              
          /**
               * 創(chuàng)建文本文件.
               * 
          @throws IOException 
               * 
               
          */

              
          public static void creatTxtFile() throws IOException{
                  
          if (!filename.exists()) {
                      filename.createNewFile();
                      System.err.println(filename 
          + "已創(chuàng)建!");
                  }

              }

              
              
          /**
               * 讀取文本文件.
               * 
               
          */

              
          public static String readTxtFile(){
                  String read;
                  FileReader fileread;
                  
          try {
                      fileread 
          = new FileReader(filename);
                      bufread 
          = new BufferedReader(fileread);
                      
          try {
                          
          while ((read = bufread.readLine()) != null{
                              readStr 
          = readStr + read+ "\r\n";
                          }

                      }
           catch (IOException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                  }
           catch (FileNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }


                  System.out.println(
          "文件內(nèi)容是:"+ "\r\n" + readStr);
                  
          return readStr;
              }

              
              
          /**
               * 寫(xiě)文件.
               * 
               
          */

              
          public static void writeTxtFile(String newStr) throws IOException{
                  
          //先讀取原有文件內(nèi)容,然后進(jìn)行寫(xiě)入操作
                  String filein = newStr + "\r\n" + readStr + "\r\n";
                  RandomAccessFile mm 
          = null;
                  
          try {
                      mm 
          = new RandomAccessFile(filename, "rw");
                      mm.writeBytes(filein);
                  }
           catch (IOException e1) {
                      
          // TODO 自動(dòng)生成 catch 塊
                      e1.printStackTrace();
                  }
           finally {
                      
          if (mm != null{
                          
          try {
                              mm.close();
                          }
           catch (IOException e2) {
                              
          // TODO 自動(dòng)生成 catch 塊
                              e2.printStackTrace();
                          }

                      }

                  }

              }

              
              
          /**
               * 將文件中指定內(nèi)容的第一行替換為其它內(nèi)容.
               * 
               * 
          @param oldStr
               *            查找內(nèi)容
               * 
          @param replaceStr
               *            替換內(nèi)容
               
          */

              
          public static void replaceTxtByStr(String oldStr,String replaceStr) {
                  String temp 
          = "";
                  
          try {
                      File file 
          = new File(path);
                      FileInputStream fis 
          = new FileInputStream(file);
                      InputStreamReader isr 
          = new InputStreamReader(fis);
                      BufferedReader br 
          = new BufferedReader(isr);
                      StringBuffer buf 
          = new StringBuffer();

                      
          // 保存該行前面的內(nèi)容
                      for (int j = 1; (temp = br.readLine()) != null
                              
          && !temp.equals(oldStr); j++{
                          buf 
          = buf.append(temp);
                          buf 
          = buf.append(System.getProperty("line.separator"));
                      }


                      
          // 將內(nèi)容插入
                      buf = buf.append(replaceStr);

                      
          // 保存該行后面的內(nèi)容
                      while ((temp = br.readLine()) != null{
                          buf 
          = buf.append(System.getProperty("line.separator"));
                          buf 
          = buf.append(temp);
                      }


                      br.close();
                      FileOutputStream fos 
          = new FileOutputStream(file);
                      PrintWriter pw 
          = new PrintWriter(fos);
                      pw.write(buf.toString().toCharArray());
                      pw.flush();
                      pw.close();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

              }

              
          /**
               * main方法測(cè)試
               * 
          @param s
               * 
          @throws IOException
               
          */

              
          public static void main(String[] s) throws IOException {
                  ReadWriteFile.creatTxtFile();
                  ReadWriteFile.readTxtFile();
                  ReadWriteFile.writeTxtFile(
          "20080808:12:13");
          //        ReadWriteFile.replaceTxtByStr("ken", "zhang");
              }

          }



          J-CN工作室
          www.j-cn.org
          posted on 2009-10-04 17:17 鐵猴 閱讀(7907) 評(píng)論(1)  編輯  收藏 所屬分類: JAVA隨記

          評(píng)論

          # re: java創(chuàng)建TXT文件并進(jìn)行讀、寫(xiě)、修改操作 [未登錄](méi) 2011-08-31 13:58 chris
          替換功能BUG太明顯了……  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 昌平区| 新营市| 南通市| 平舆县| 内江市| 光山县| 白银市| 潜山县| 尚志市| 衡东县| 屏东市| 大宁县| 香河县| 明水县| 莆田市| 上栗县| 吉水县| 大庆市| 石河子市| 互助| 江安县| 册亨县| 上饶市| 军事| 华安县| 门头沟区| 瓮安县| 丰都县| 慈溪市| 孟村| 嘉义市| 竹山县| 秀山| 都昌县| 临洮县| 塔河县| 巨野县| 简阳市| 策勒县| 施甸县| 城固县|