java文件工具类

来源:互联网 发布:我的世界安装java 编辑:程序博客网 时间:2024/05/01 11:59

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author yinjin
 * @version 1.0
 */
public class FileUtil {
 public static void rmdirs(File path) {
  try {
   if (path.isFile()) {
    path.delete();
    return;
   }
   File[] subs = path.listFiles();
   if (subs.length == 0) {
    path.delete();
   }
   else {
    for (int i = 0; i < subs.length; i++) {
     rmdirs(subs[i]);
    }
    path.delete();
   }
  }
  catch (Exception ex) {
  }
 }
 public static void xcopy(File src,File destine)
 {
  try {
   if (!src.exists() || src.getCanonicalPath().equals(destine.getCanonicalPath())) return;
  }
  catch (IOException ex) {
  }
  File[] chs = src.listFiles();
  for (int i=0;i<chs.length;i++)
  {
   if (chs[i].isFile())
   {
    File destineFile = new File(destine,chs[i].getName());
    copy (chs[i],destineFile);
   }
   else
   {
    File destineDir = new File(destine,chs[i].getName());
    destineDir.mkdirs();
    xcopy(chs[i],destineDir);
   }
  }
 }

 public static void copy(File src,File destine)
 {
  try{
   if (!src.exists() || src.getCanonicalPath().equals(destine.getCanonicalPath())) return;
   FileInputStream fins = new FileInputStream(src);
   copy(fins,destine);
   destine.setLastModified(src.lastModified());
  }
  catch(Exception e)
  {}
 }
 public static void copy(InputStream fins, File destine){
  try {
   if (fins == null) return;
   destine.getParentFile().mkdirs();
   FileOutputStream fos = new FileOutputStream(destine);
   byte[] buf = new byte[1024];
   int readLen;
   while ((readLen = fins.read(buf,0,buf.length))>0)
   {
    fos.write(buf,0,readLen);
   }
   fos.flush();
   fos.close();
   fins.close();
  }
  catch (Exception ex) {
  }
 }
 public static void move(File src,File destine){
  try {
   if (!src.exists() || src.getCanonicalPath().equals(destine.getCanonicalPath())) return;
  }catch(IOException ex){}
  copy(src,destine);
  src.delete();
 }
 /**
  * 重命名文件而不改变后缀
  * @param fromFilename:要重命名的文件名
  * @param toFilename:新的文件名,没有后缀
  */
 public static String rename(String fromFilename,String toFilename)
 {
  String filename = toFilename;
  int tail_index = fromFilename.lastIndexOf(".");
  if(tail_index != -1)
  {
      String tail = fromFilename.substring(tail_index);  
      filename = toFilename+tail;     
  }
  return filename;
  
 }
 /**
  * 根据给定的路径创建目录
  * @param dest
  * @return 成功返回true,失败返回false
  */
 public static boolean makeDir(String dest)
 {
  boolean flag = true;
  try
  {
      File f = new File(dest);   
   if(!f.mkdirs())
   {
    flag = false;
   }   
  }
  catch(Exception e)
  {
   e.printStackTrace();
   flag = false;
  }
  return flag;
 }
 /**
  * 测试方法
  * @param args
  */
 public static void main(String []args)
 {
  String f = "U765P8T1D110044F913DT20040912232529";
  String e=FileUtil.rename(f,"修改");
  System.out.println("源文件名称:"+f+"/n经过修改以后的文件名:"+e);
  String path ="E://BJhouseApplicationsafasdf//bjhouseHomeasdf//configasdf";
  String lname = DateUtil.getTimeString();
  path += File.separator+lname;
  if(FileUtil.makeDir(path))
  {
   System.out.println("目录创建成功");
  }
  else
      System.out.println("目录创建失败");
  
 } 
}

 

原创粉丝点击