隨筆-314  評(píng)論-209  文章-0  trackbacks-0

          業(yè)務(wù)常用,供大家學(xué)習(xí):

          引用地址:http://www.easydone.cn/014/200604022353065155.htm

          package org.easydone.file;



          import java.io.File;
          import java.io.FileWriter;
          import java.io.IOException;
          import java.io.PrintWriter;
          import java.text.SimpleDateFormat;
          import java.util.Date;

          /**
           * <p>Title: File 常用操作(部分)</p>
           * <p>Description: 業(yè)務(wù)用</p>
           * <p>Copyright: Copyright (c) 2006 www.easydone.cn</p>
           * <p>Company: 北京聚能易成科技有限公司</p>
           * @authory dirboy
           * @version 1.0
           */

          public class FileOperate {

              /**
              * 創(chuàng)建目錄
              * @param folderPath:目錄路徑
              * @return
              * @throws IOException
              */
              public static boolean createFolder(String folderPath) throws IOException {
                  boolean result = false;
                  File f = new File(folderPath);
                  result = f.mkdirs();
                  return result;
              }
              /**
              * 刪除目錄下所有文件
              * @param directory (File 對(duì)象)
              */
              public void emptyDirectory(File directory) {
                  File[] entries = directory.listFiles();
                  for (int i = 0; i < entries.length; i++) {
                      entries[i].delete();
                  }
              }


              /**
              * 創(chuàng)建文件
              * @param filepath:文件所在目錄路徑,比如:c:/test/test.txt
              * @return
              */
              public static boolean makeFile(String filepath) throws IOException {
                  boolean result = false;
                  File file = new File(filepath);
                  result = file.createNewFile();
                  file = null;
                  return result;
              }
              /**
              * 刪除文件
              * @param filepath:文件所在物理路徑
              * @return
              */
              public static boolean isDel(String filepath) {
                  boolean result = false;
                  File file = new File(filepath);
                  result = file.delete();
                  file = null;
                  return result;
              }
              /**
              * 文件重命名
              * @param filepath:文件所在物理路徑
              * @param destname:新文件名
              * @return
              */
              public static boolean renamefile(String filepath,String destname) {
                  boolean result = false;
                  File f = new File(filepath);
                  String fileParent = f.getParent();
                  String filename = f.getName();
                  File rf = new File(fileParent+"http://"+destname);
                  if(f.renameTo(rf)) {
                      result = true;
                  }
                  f = null;
                  rf = null;
                  return result;
              }
              /**
              * 將文件內(nèi)容寫入數(shù)據(jù)庫中
              * @param filepath:文件所在物理路徑
              * @param content:寫入內(nèi)容
              * @throws Exception
              */
              public static void WriteFile(String filepath,String content) throws Exception {
                  FileWriter filewriter = new FileWriter(filepath,true);//寫入多行
                  PrintWriter printwriter = new PrintWriter(filewriter);
                  printwriter.println(content);
                  printwriter.flush();
                  printwriter.close();
                  filewriter.close();
              }
              /**
              * 日志備份
              * @param filePath:日志備份路徑
              * @param baksize:日志備份大小參考值(字節(jié)大小)
              * @throws IOException
              */
              public static void logBak(String filePath,long baksize) throws IOException {
                  File f = new File(filePath);
                  long len = f.length();
                  SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");
                  String s = simpledateformat.format(new Date());
                  String fileName = f.getName();
                  int dot = fileName.indexOf(".");
                  String bakName = s+fileName.substring(dot);
                  System.out.println(bakName);
                  if(len>=baksize) {
                      renamefile(filePath,bakName);
                      makeFile(filePath);
                  }
                  f = null;
              }

          }

          posted on 2007-06-14 16:52 xzc 閱讀(2059) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 嵩明县| 五河县| 襄樊市| 宁武县| 准格尔旗| 盖州市| 汝城县| 千阳县| 库车县| 微博| 海阳市| 龙陵县| 西充县| 中方县| 兴仁县| 远安县| 竹溪县| 福安市| 浑源县| 高陵县| 三亚市| 贡山| 邵东县| 土默特左旗| 凤台县| 聂拉木县| 罗源县| 淳化县| 大同市| 枣庄市| 秦安县| 吴江市| 黄梅县| 阳东县| 永胜县| 定结县| 华亭县| 清水县| 荔浦县| 陆河县| 安化县|