Copy文件工具类

/**
 * Copy文件工具类
 * @author WuYingbin
 *
 */
public class FileUtil {
	/**
	 * 路径替换如:\换成/
	 * 
	 * @param dir
	 * @return
	 */
	public static String replaceDir(String dir) {
		String newDir = dir.replace("\\", "/");
		return newDir;
	}

	/**
	 * 根据路径创建文件或文件夹
	 * 
	 * @param path
	 * @return{
	 */
	public static boolean createFileOrDirByPath(String path) {
		boolean flag = false;

		return flag;
	}

	/**
	 * 复制文件到指定目录,不存在的话创建该目录
	 * 
	 * @param sourceFile
	 * @param targetFile
	 * @throws IOException
	 */
	public static void copyFile(File sourceFile, File targetFile)
			throws IOException {
		// 新建文件输入流并对它进行缓冲
		FileInputStream input = new FileInputStream(sourceFile);
		BufferedInputStream inBuff = new BufferedInputStream(input);

		// 判断目标目录是否存在,不存在建立目录
		if (!targetFile.getParentFile().exists()) {
			targetFile.getParentFile().mkdirs();
		}

		// 新建文件输出流并对它进行缓冲
		FileOutputStream output = new FileOutputStream(targetFile);
		BufferedOutputStream outBuff = new BufferedOutputStream(output);

		// 缓冲数组
		byte[] b = new byte[1024 * 5];
		int len;
		while ((len = inBuff.read(b)) != -1) {
			outBuff.write(b, 0, len);
		}
		// 刷新此缓冲的输出流
		outBuff.flush();
		
		// 关闭流
		inBuff.close();
		outBuff.close();
		output.close();
		input.close();
		
		//不改变最后修改时间
		targetFile.setLastModified(sourceFile.lastModified());
	}

	/**
	 * 复制文件夹到指定目录
	 * @param sourceDir
	 * @param targetDir
	 * @throws IOException
	 */
	public static void copyDirectiory(String sourceDir, String targetDir)
			throws IOException {
		// 新建目标目录
		(new File(targetDir)).mkdirs();
		// 获取源文件夹当前下的文件或目录
		File[] file = (new File(sourceDir)).listFiles();
		System.out.println(file==null);
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {
				// 源文件
				File sourceFile = file[i];
				// 目标文件
				File targetFile = new File(new File(targetDir)
						.getAbsolutePath()
						+ File.separator + file[i].getName());
				copyFile(sourceFile, targetFile);
			}
			if (file[i].isDirectory()) {
				// 准备复制的源文件夹
				String dir1 = sourceDir + "/" + file[i].getName();
				// 准备复制的目标文件夹
				String dir2 = targetDir + "/" + file[i].getName();
				copyDirectiory(dir1, dir2);
			}
		}
	}
	/**
	 * 复制文件夹到指定目录,指定日期date的days天之内的
	 * @param sourceDir
	 * @param targetDir
	 * @throws IOException
	 */
	public static void copyDirectioryByDate(String sourceDir, String targetDir,Date date,int days)
			throws IOException {
		// 新建目标目录
		(new File(targetDir)).mkdirs();
		// 获取源文件夹当前下的文件或目录
		File[] file = (new File(sourceDir)).listFiles();
//		System.out.println(file==null);
		if(file!=null&&file.length>0){
			for (int i = 0; i < file.length; i++) {
				if (file[i].isFile()&&isRightDate(file[i], date, days)) {
					// 源文件
					File sourceFile = file[i];
					// 目标文件
					File targetFile = new File(new File(targetDir)
							.getAbsolutePath()
							+ File.separator + file[i].getName());
					copyFile(sourceFile, targetFile);
				}
				if (file[i].isDirectory()) {
					// 准备复制的源文件夹
					String dir1 = sourceDir + "/" + file[i].getName();
					// 准备复制的目标文件夹
					String dir2 = targetDir + "/" + file[i].getName();
					copyDirectioryByDate(dir1, dir2,date,days);
				}
			}
		}
		
	}
	/**
	 * 判断一个文件是否在指定时间(date)之内(days天)
	 * @param f
	 * @param date
	 * @param days
	 * @return
	 */
	public static boolean isRightDate(File f,Date date,int days){
		boolean flag = false;
		long fileTime = f.lastModified();
		Date lastModifyTime = new Date(fileTime);
		double length = DateTool.dateSubtract(date, lastModifyTime);
		System.out.println(f.getPath()+"最后修改时间"+DateTool.getDateTime(lastModifyTime));
		if(length<days){
			
			flag = true;
		}
		return flag;
	}
	/**
	 * 获取目录路径中最深一层的目录名
	 * @param dir
	 * @return
	 */
	public static String getLastDir(String dir){
		dir = dir.replace("/", "\\");
		String s = dir.substring(dir.lastIndexOf("\\")+1);
		if(s.length()==0){
			s = dir.substring(0,1);
		}
		return s;
	}
	
	/**
	 * 获取文件的创建日期
	 * @param _file
	 * @return
	 */
	public static String getFileCreateDate(File _file){
        File file=_file;        
        try{
         Process ls_proc = Runtime.getRuntime().exec("cmd.exe /c dir " + file.getAbsolutePath() + " /tc");
         DataInputStream in = new DataInputStream(ls_proc.getInputStream());
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
         for (int i = 0; i < 5; i++ )
         {
          br.readLine();
         }
         String stuff = br.readLine();
         //System.out.println(stuff);
         StringTokenizer st = new StringTokenizer(stuff);
         String dateC = st.nextToken();
         String  time=st.nextToken();
         in.close();
         return dateC+" "+time+":00";
         }catch (Exception e){
          return null;
         }
 }
	/**     
	 *递归删除目录下的所有文件及子目录下所有文件    
	 *@param dir 将要删除的文件目录     
	 *@return boolean Returns "true" if all deletions were successful.   
	 *If a deletion fails, the method stops attempting to    
	 * delete and returns "false".   
     */    
	 public static boolean deleteDir(File dir) {       
		 if (dir.isDirectory()) {            
			 String[] children = dir.list();
			 //递归删除目录中的子目录下            
			 for (int i=0; i<children.length; i++) { 
				 boolean success = deleteDir(new File(dir, children[i]));               
				 if (!success) {                    
					 return false;                
				 }            
			 }        
		}        
		  // 目录此时为空,可以删除        
	 return dir.delete();    
	 }    
	 /**
	  * 遍历文件夹下所有文件并返回文件列表
	  * @param dir
	  * @param files
	  * @return
	  */
	 public static List<File> traversalFolder(String dir,List<File> files){
		File f = new File(dir);
		File[]	fs = f.listFiles();
		for(File ff:fs){
			if(ff.isDirectory()){
				traversalFolder(ff.getPath(), files);
			}else{
				files.add(ff);
			}
		}
		return files;
	 }

	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值