巷尾的酒吧

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            64 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

          package com.abin.facade.ws.mail.function;

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.FileReader;
          import java.io.RandomAccessFile;

          public class FileOperation {
           
           /**
            * 創建文件
            * @param fileName
            * @return
            */
           public static boolean createFile(File fileName)throws Exception{
            boolean flag=false;
            try{
             if(!fileName.exists()){
              fileName.createNewFile();
              flag=true;
             }
            }catch(Exception e){
             e.printStackTrace();
            }
            return true;
           }
           
           /**
            * 讀TXT文件內容
            * @param fileName
            * @return
            */
           public static String readTxtFile(File fileName)throws Exception{
            String result=null;
            FileReader fileReader=null;
            BufferedReader bufferedReader=null;
            try{
             fileReader=new FileReader(fileName);
             bufferedReader=new BufferedReader(fileReader);
             try{
              String read=null;
              while((read=bufferedReader.readLine())!=null){
               result=result+read+"\r\n";
              }
             }catch(Exception e){
              e.printStackTrace();
             }
            }catch(Exception e){
             e.printStackTrace();
            }finally{
             if(bufferedReader!=null){
              bufferedReader.close();
             }
             if(fileReader!=null){
              fileReader.close();
             }
            }
            System.out.println("讀取出來的文件內容是:"+"\r\n"+result);
            return result;
           }
           
           
           public static boolean writeTxtFile(String content,File  fileName)throws Exception{
            RandomAccessFile mm=null;
            boolean flag=false;
            FileOutputStream o=null;
            try {
             o = new FileOutputStream(fileName);
                o.write(content.getBytes("GBK"));
                o.close();
          //   mm=new RandomAccessFile(fileName,"rw");
          //   mm.writeBytes(content);
             flag=true;
            } catch (Exception e) {
             // TODO: handle exception
             e.printStackTrace();
            }finally{
             if(mm!=null){
              mm.close();
             }
            }
            return flag;
           }


          public static void contentToTxt(String filePath, String content) {
                  String str = new String(); //原有txt內容
                  String s1 = new String();//內容更新
                  try {
                      File f = new File(filePath);
                      if (f.exists()) {
                          System.out.print("文件存在");
                      } else {
                          System.out.print("文件不存在");
                          f.createNewFile();// 不存在則創建
                      }
                      BufferedReader input = new BufferedReader(new FileReader(f));

                      while ((str = input.readLine()) != null) {
                          s1 += str + "\n";
                      }
                      System.out.println(s1);
                      input.close();
                      s1 += content;

                      BufferedWriter output = new BufferedWriter(new FileWriter(f));
                      output.write(s1);
                      output.close();
                  } catch (Exception e) {
                      e.printStackTrace();

                  }
              }

           
           
          }

          posted on 2011-11-16 13:33 abing 閱讀(2820) 評論(2)  編輯  收藏

          Feedback

          # re: Java 創建Txt文件,寫入文件內容,讀取文件內容 2012-07-30 12:49 陳瑞玲陳瑞玲
          你的代碼缺少主函數  回復  更多評論
            

          # re: Java 創建Txt文件,寫入文件內容,讀取文件內容[未登錄] 2012-08-15 00:29 abin
          我這是寫好,以便其他地方調用的,不是鬧著玩的,呵呵  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 安化县| 郧西县| 且末县| 凌海市| 河南省| 云和县| 水城县| 神池县| 青川县| 玛纳斯县| 浦县| 康保县| 余江县| 巫溪县| 周至县| 漠河县| 锡林郭勒盟| 洛南县| 印江| 固安县| 阿瓦提县| 临潭县| 雅江县| 呼玛县| 新营市| 始兴县| 分宜县| 温泉县| 类乌齐县| 渭源县| 湖州市| 昭苏县| 河津市| 左贡县| 扬中市| 阿克陶县| 彭州市| 顺平县| 彩票| 阿瓦提县| 松桃|