用Java实现对文件夹的操作

1、复制文件夹

private void copyFile(File sourceFile,File targetFile){
		try {
			if(!sourceFile.exists()){
				return;
			}else{
				if(sourceFile.isDirectory()){
					if(!targetFile.exists()){
						targetFile.mkdirs();
					}
					File[] list=sourceFile.listFiles();
					for(File f:list){
						File newFile=new File(targetFile,f.getName());
						copyFile(f, newFile);
					}
				}else{
					if(!targetFile.exists()){
						targetFile.createNewFile();
					}
					FileInputStream input = new FileInputStream(sourceFile);
					BufferedInputStream inBuff = new BufferedInputStream(input); // 新建文件输出流并对它进行缓冲
					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();
				}
			}
		} catch (Exception e) {
			Tools.errorList(e, "复制文件");
		}
	}
2、删除文件夹
private void deleteFile(File file) {
		if(file.exists()){
			if(file.isFile()){
				file.delete();
			}else if (file.isDirectory()) {
				File files[] = file.listFiles();               //声明目录下所有的文件 files[];
				for(int i=0;i<files.length;i++){            //遍历目录下所有的文件
					deleteFile(files[i]);             //把每个文件 用这个方法进行迭代
			    } 
			}
			file.delete();
		}
	}
3、压缩文件夹

File XMFile=new File(filePath+"fileName"); 
File[] fileList = XMFile.listFiles();
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(filePath+"<span style="font-family: Arial, Helvetica, sans-serif;">fileName.</span>zip"));
if (fileList.length!=0) {
for (int i = 0; i < fileList.length; i++) {
ZIP.zipFile(zos, fileList[i], "");
}
}
zos.close();
  
    ZIP类中的方法:
<pre name="code" class="html">public static void zipFile(ZipOutputStream output, File file, String basePath) throws Exception {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            if (file.isDirectory()) {// 文件为目录
                File list[] = file.listFiles();
                basePath = basePath + (basePath.length() == 0 ? "" : "/") + file.getName();
                for (File f : list) {
                    zipFile(output, f, basePath);
                }
            } else {// 压缩文件
                basePath = (basePath.length() == 0 ? "" : basePath + "/") + file.getName();
                output.putNextEntry(new ZipEntry(basePath));
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                byte[] buf = new byte[1024];
                int len;
                while ((len = bis.read(buf)) != -1) {
                    output.write(buf, 0, len);
                }
                output.flush();
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (bis != null) {
                bis.close();
            }

            if (fis != null) {
                fis.close();
            }
        }
    }


 
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">4、解压zip文件</span>

public static void upZIP(File unZipFile,File unZipFolder) throws Exception{
		  InputStream input = null; 
		  OutputStream output = null;  
		  ZipFile zipFile = null;
		  try {
				zipFile = new ZipFile(unZipFile);  // 创建zip文件对象
				Enumeration zipEnum = zipFile.getEntries();  // 得到zip文件条目枚举对象  
				ZipEntry entry = null;  // 定义对象  
				String entryName = null, path = null;  
				String names[] = null;  
				int length;  
				while (zipEnum.hasMoreElements()) {  // 循环读取条目  
				    entry = (ZipEntry) zipEnum.nextElement();  // 得到当前条目  
				    entryName = new String(entry.getName());  
				    // 用/分隔条目名称  
				    names = entryName.split("\\/");  
				    length = names.length;  
				    path = unZipFolder.getAbsolutePath();
				    for (int v = 0; v < length; v++) {  
				    	if (v < length - 1){ // 最后一个目录之前的目录 
				    		File tempFile=new File(path += "/" + names[v] + "/");
				    		if(!tempFile.exists()){
				    			tempFile.mkdirs();
				    		}
				    	}else { // 最后一个  
				    		if (entryName.endsWith("/")){ // 为目录,则创建文件夹  
					    		File tempFile=new File(unZipFolder.getAbsolutePath()+ "/" + entryName);
					    		if(!tempFile.exists()){
					    			tempFile.mkdirs();
					    		}
				    		}else { // 为文件,则输出到文件  
				    			input = zipFile.getInputStream(entry);  
				    			output = new FileOutputStream(new File(unZipFolder.getAbsolutePath()+ "/" + entryName));  
				    			byte[] buffer = new byte[1024 * 8];  
				    			int readLen = 0;  
				    			while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {
				    				output.write(buffer, 0, readLen);  
				    			}
				    			if (input != null)  
				    				input.close();  
				    			if (output != null) {  
				    				output.flush();  
				    				output.close();  
				    			}
				    		}
				    	}  
				    }  
				}  
		  } catch (Exception ex) {  
		  		throw ex;
		  } finally {  
			   try {
				   if (input != null)  
				    input.close();  
				   if (output != null) {  
				    output.flush();  
				    output.close();  
				   }
				   if(zipFile!=null){
					zipFile.close(); 
				   }
			} catch (Exception e) {
			}
		  } 
	}
5、把文件写成二进制流字符串

public static String ZIPtoSTR(File file) throws Exception{
		 StringBuffer suf = new StringBuffer();
		 FileInputStream fis = null;
                 int temp = 0;
                 fis = new FileInputStream(file);
                 while((temp = fis.read()) != -1){
        	<span style="white-space:pre">	</span>suf.append(temp);
        	<span style="white-space:pre">	</span>suf.append(",");
        <span style="white-space:pre">	</span> }
         <span style="white-space:pre">	</span>fis.close();
		return suf.toString();
	 }
6、把二进制流转换为文件

public static File STRtoZIP(String str,File file) throws Exception{
		 FileOutputStream fos = new FileOutputStream(file);
		 String[] str_arr = str.split(",");
		 for(String v:str_arr){
			fos.write(Integer.valueOf(v));
		 }
		 fos.close();
		 return file;
	 }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值