文件夹的操作

package com.dartfar.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
* @Copyright 2007 All right reserved
* @version 创建时间:Created on Nov 10, 2007
* @author 作者:Create by zhen
* @Email: wang99567@163.com
* @QQ: 275159177
* 类说明
* @description 文件操作类
*/
public class FileOP {
/**
* 缓存的大小
*/
private static int BUFFER_SIZE=800*1024;
/**
* 转移文件
* @param src
* @param dst
*/
public static boolean copy(File src, File dst) {
   try {
    InputStream in = null;
    OutputStream out = null;
    try {
     in = new BufferedInputStream(new FileInputStream(src),
       BUFFER_SIZE);
     out = new BufferedOutputStream(new FileOutputStream(dst),
       BUFFER_SIZE);
     byte[] buffer = new byte[BUFFER_SIZE];
     while (in.read(buffer) > 0) {
      out.write(buffer);
     }
    } finally {
     if (null != in) {
      in.close();
     }
     if (null != out) {
      out.close();
     }
    }
    return true;
   } catch (Exception e) {
    e.printStackTrace();
    return false;
   }
}
/**
* 通过路径删除文件
* @param src
* @return
*/
public static boolean deleteFile(String src){
   File delFile = new File(src);
   try {
    /**
    * 如果存在并且是文件,那么就删除它
    * 否则输出文件不存在,
    * 返回false
    */
    if (delFile.exists()&&delFile.isFile()) {
     return delFile.delete();
    }else{
     System.out.println("你要删除的文件不存在!");
     return false;
    }
   } catch (Exception e) {
    e.printStackTrace();
    return false;
   }
}
/**
* 通过文件实例删除文件
* @param src
* @return
*/
public static boolean deleteFile(File src){
   try {
    /**
    * 如果存在并且是文件,那么就删除它
    * 否则输出文件不存在,
    * 返回false
    */
    if (src.exists()&&src.isFile()) {
     return src.delete();
    }else{
     System.out.println("你要删除的文件不存在!");
     return false;
    }
   } catch (Exception e) {
    e.printStackTrace();
    return false;
   }
}

/**
* 通过文件实例删除文件夹
* @param src
* @return
* 用递归的方法实现文件夹的删除
*/
public static boolean deleteFolder(File src){
   try {
    /**
    * 先判断一下文件夹是否存在
    */
    if(!src.exists()){
     System.out.println("你要删除的文件夹不存在!");
    }
    /**
    * 如果是一个文件夹,那么先删除子文件或者子文件夹。
    * 如果是文件那么直接删除文件。
    */
    if (src.isDirectory()) {
     File[] files = src.listFiles();
     for(File file:files){
      if(file.exists()&&file.isFile()){
       file.delete();
      }else{
       deleteFolder(file);
      }
     }  
    } else if (src.isFile()) {
     src.delete();
    }
    return true;
   } catch (Exception e) {
    e.printStackTrace();
    return false;
   }
}

/**
* 通过路径删除文件夹
* @param src
* @return
* 用递归的方法实现文件夹的删除
*/
public static boolean deleteFolder(String src){
   File file = new File(src);
   /**
   * 先判断一下文件夹是否存在
   */
   if(!file.exists()){
    System.out.println("你要删除的文件夹不存在!");
   }
   try {
    /**
    * 如果是一个文件夹,那么先删除子文件或者子文件夹。
    * 如果是文件那么直接删除文件。
    */
    if (file.isDirectory()) {
     File[] files = file.listFiles();
     for(File f:files){
      if(f.exists()&&f.isFile()){
       f.delete();
      }else{
       deleteFolder(f);
      }
     }  
    } else if (file.isFile()) {
     file.delete();
    }
    return true;
   } catch (Exception e) {
    e.printStackTrace();
    return false;
   }
}

/**
* 得到文件的扩展名
* @param fileName
* @return
*/
public static String getExtention(String fileName){
   int pos=fileName.lastIndexOf(".");
   return fileName.substring(pos);
}
public static int getBUFFER_SIZE() {
   return BUFFER_SIZE;
}
public static void setBUFFER_SIZE(int buffer_size) {
   BUFFER_SIZE = buffer_size;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值