<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    學習 學習 再學習

    學習才是唯一的出路

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      31 隨筆 :: 1 文章 :: 8 評論 :: 0 Trackbacks
    import java.io.*;


    /**
     * 
     * 功能描述:創建TXT文件并進行讀、寫、修改操作
     *      
     * 
    @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 ="";


        
    /**
         * 創建文本文件.
         * 
    @throws IOException 
         * 
         
    */

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

        }

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

        
    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(
    "文件內容是:"+ "\r\n" + readStr);
            
    return readStr;
        }

        
        
    /**
         * 寫文件.
         * 
         
    */

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

                }

            }

        }

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

        
    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();

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


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

                
    // 保存該行后面的內容
                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方法測試
         * 
    @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 鐵猴 閱讀(7900) 評論(1)  編輯  收藏 所屬分類: JAVA隨記

    評論

    # re: java創建TXT文件并進行讀、寫、修改操作 [未登錄] 2011-08-31 13:58 chris
    替換功能BUG太明顯了……  回復  更多評論
      

    主站蜘蛛池模板: 免费在线视频你懂的| 成年18网站免费视频网站| 亚洲日韩乱码中文无码蜜桃臀| 91免费精品国自产拍在线不卡| 美女被暴羞羞免费视频| 亚洲AV无码乱码国产麻豆穿越 | 午夜精品免费在线观看| 亚洲高清有码中文字| 亚洲精品无码久久久| 巨波霸乳在线永久免费视频 | 亚洲精品无码专区2| 久久久久久毛片免费播放| 丰满亚洲大尺度无码无码专线| 亚洲春色在线视频| 日本免费中文字幕在线看| 日韩免费视频一区二区| 无码天堂va亚洲va在线va| 亚洲精品视频久久| 中文字幕不卡亚洲| 好吊妞在线成人免费| 精品国产污污免费网站| 国产亚洲精品欧洲在线观看| 亚洲精品在线播放| 亚洲中文字幕不卡无码| 无码国模国产在线观看免费| 91禁漫免费进入| A级毛片成人网站免费看| 亚洲精华液一二三产区| 1区1区3区4区产品亚洲| 亚洲中文久久精品无码ww16| 日韩免费观看的一级毛片| 亚洲精品视频在线免费| 国产亚洲精品免费视频播放| 久久亚洲中文字幕无码| 狠狠色香婷婷久久亚洲精品| 亚洲第一视频网站| 亚洲第一极品精品无码久久| 亚洲国产小视频精品久久久三级| 拨牐拨牐x8免费| 久久精品无码一区二区三区免费| 久久久久国产精品免费看|