文件操作工具类

文件创建,

目录新建,

文件删除,

文件压缩,

文件解压


/**
	 * 创建新文件
	 * @param newFilePath
	 * @return 成功返回true,失败返回false
	 */
	public static Boolean createNewFile(String newFilePath){
		return createNewFile(newFilePath,false);
	}
	/**
	 * 创建新文件
	 * @param newFilePath	新文件路径
	 * @param canOver	是否可以覆盖,true文件存在时覆盖,false文件存在时不覆盖
	 * @return	成功返回true,失败返回false
	 */
	public static Boolean createNewFile(String newFilePath,Boolean canOver ){
		try {
			File file=new File(newFilePath);
			if(file.exists()&&canOver){//文件存在,并且可以覆盖
				file.delete();
				System.out.println("文件:"+newFilePath+"[已经存在].....正在删除重建");
			}else if(file.exists()&&!canOver){//文件存在,并且不可以覆盖
				System.out.println("文件:"+newFilePath+"[已经存在,创建失败]");
				return false;
			}
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			
			return file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
		
	}
	/**
	 * 删除文件或者目录
	 * @param srcFilePaht
	 * @return
	 */
	public static Boolean dellFileOrDir(String srcFilePaht){
		File file=new File(srcFilePaht);
		if(!file.exists()){
			System.out.println("文件或目录:"+srcFilePaht+"[不存在,删除失败]");
			return false;
		}
		if(file.isFile()){
			return file.delete();
		}else if(file.isDirectory()){
			File[] childFiles=file.listFiles();
			boolean flag=true;
			for(File childFile : childFiles){
				if(!dellFileOrDir(childFile.getAbsolutePath())){
					System.out.println("文件:"+childFile+"[删除失败]");
					flag=false;
				}
			}
			if(flag){
				file.delete();
			}else{
				System.out.println("目录:"+file.getAbsolutePath()+"[删除失败]");
				return false;
			}
		}
		return true;
	}

	/**
	 * 创建文件夹
	 * @param dirPath
	 * @return
	 */
	public static Boolean createDir(String dirPath){
		File file=new File(dirPath);
		if(!file.mkdir()){
			System.out.println("文件夹:"+dirPath+"[创建失败]");
		}
		return true;
	}
	
	/**
	 * 创建文件夹,包括父路径
	 * @param dirPath
	 * @return
	 */
	public static Boolean createDirs(String dirPath){
		File file=new File(dirPath);
		if(!file.mkdirs()){
			System.out.println("文件夹:"+dirPath+"[创建失败]");
		}
		return true;
	}
	/**
	 * 压缩文件/文件夹  操作
	 * @param srcFilePath	需要压缩的文件的文件路径
	 * @param targetFilePath	压缩后的文件名称
	 * @return
	 */
	public static Boolean zipFile(String srcFilePath,String targetFilePath){
		ZipOutputStream zipOut=null;
		try {
			File srcFile=new File(srcFilePath);
			File targetFile= new File(targetFilePath);
			if(!targetFile.getParentFile().exists()){
				targetFile.getParentFile().mkdirs();
			}
			zipOut = new ZipOutputStream(new FileOutputStream(targetFile)) ; 
			zipOut.setComment("zip格式压缩文件");
			iterationMakeZipFile(srcFile, zipOut,srcFile.getName());
			zipOut.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			if(zipOut!=null){
				try {
					zipOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}
	/**
	 * 真正的压缩文件操作,迭代所有文件压缩
	 * @param srcFile	
	 * @param zipOut
	 * @param parentName
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private static void iterationMakeZipFile(File srcFile, ZipOutputStream zipOut,String parentName)
			throws FileNotFoundException, IOException {
		if(srcFile.isFile()){
			InputStream input = new FileInputStream(srcFile);
			if(parentName!=null){
				//ZipEntry的名字体现了压缩文件解压以后的层级关系
				zipOut.putNextEntry(new ZipEntry(parentName+File.separator+srcFile.getName()) );  // 设置ZipEntry对象  
			}else{
				
				zipOut.putNextEntry(new ZipEntry(srcFile.getName()) );  // 设置ZipEntry对象  
			}
			 int length=0;
			 byte[] temp=new byte[1024];
			 while((length=input.read(temp))>0){
				 zipOut.write(temp, 0, length);
			 }
			 input.close();
		}else{
			File[] fileList=srcFile.listFiles();
			for(File file :fileList){
				iterationMakeZipFile(file, zipOut,parentName+(!file.isDirectory()?"":File.separator+file.getName()));
			}
		}
		
	}
	
	/**
	 * 解压文件
	 * 默认是utf-8编码
	 * 其他编码会报错
	 * @param srcFilePath	待解压的文件
	 * @param targetFilePath	解压到哪个文件夹
	 * @return
	 */
	public static Boolean unZipFile(String srcFilePath,String targetFilePath){
		try {
			File targetFile=new File(targetFilePath);
			if(!targetFile.exists()){
				targetFile.mkdirs();
			}
			@SuppressWarnings("resource")
			ZipFile zipFile=new ZipFile(srcFilePath,Charset.forName("GBK"));
			Enumeration<? extends ZipEntry> entrys=zipFile.entries();
			while (entrys.hasMoreElements()) {
				InputStream in=null;
				OutputStream out=null;
				try {
					ZipEntry entry = (ZipEntry) entrys.nextElement();
					in=zipFile.getInputStream(entry);
					File outfile=new File(targetFilePath+File.separator+entry.getName());
					//if(entry.getName().endsWith("\\")||entry.getName().endsWith("/")){//表示是文件夹
					if(entry.isDirectory()){
						outfile.mkdirs();
					}else{
						if(!outfile.getParentFile().exists()){
							outfile.getParentFile().mkdirs();
						}
						out=new FileOutputStream(outfile);
						int length=0;
						byte[] temp=new byte[1024];
						while((length=in.read(temp))>0){
							out.write(temp, 0, length);
						}
					}
					/*if(!outfile.getParentFile().exists()){
						outfile.getParentFile().mkdirs();
					}
					if(!outfile.exists()){
						outfile.createNewFile();
					}*/
					
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}catch (Exception e) {
					e.printStackTrace();
				}
				finally {
					if(out!=null){
						out.close();
					}
					if(in!=null){
						in.close();
					}
				}
			}
			zipFile.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
		
	}
	/**
	 * 拷贝文件
	 * @param srcFilePath	源文件文件路径
	 * @param targetFilePath	目标文件路径
	 * @return
	 * @throws Exception
	 */
	public static Boolean copyFile(String srcFilePath,String targetFilePath) throws Exception{
		return copyFile(new File(srcFilePath), new File(targetFilePath));
	}
	/**
	 * 拷贝文件
	 * @param srcFile	源文件
	 * @param targetFile	目标文件
	 * @throws Exception 
	 */
	private static Boolean copyFile(File srcFile , File targetFile) throws Exception {
		if(srcFile.isDirectory()){
			File dirFile=new File(targetFile.getAbsolutePath()+File.separator+srcFile.getName());
			dirFile.mkdirs();
			File[] files=srcFile.listFiles();
			for(File file  : files){
				copyFile(file,new File(dirFile+File.separator+file.getName()));
			}
		}else{
			if(!targetFile.getParentFile().exists()){
				targetFile.getParentFile().mkdirs();
			}
			
			InputStream is=null;
			OutputStream os=null;
			try {
				is=new FileInputStream(srcFile);
				os=new FileOutputStream(targetFile);
				int length=0;
				byte[] temp=new byte[1024];
				while((length=is.read(temp))>0){
					os.write(temp, 0, length);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}finally {
				if(os!=null){
					os.close();
				}
				if(is!=null){
					is.close();
				}
			}
		}
		return true;
	}
	/**
	 * 输出到文件
	 * @param targetFilePath	目标文件路径
	 * @param is	输入流
	 * @return
	 */
	public static Boolean writeFile(String targetFilePath,InputStream is){
		return writeFile(new File(targetFilePath),is);
	}
	
	/**
	 * 输出到文件
	 * @param targetFile	目标文件
	 * @param is	输入流
	 * @return
	 */
	public static Boolean writeFile(File targetFile,InputStream is){
		if(!targetFile.getParentFile().exists()){
			targetFile.getParentFile().mkdirs();
		}
		OutputStream os=null;
		try {
			os=new FileOutputStream(targetFile);
			int length=0;
			byte[] temp=new byte[1024];
			while((length=is.read(temp))>0){
				os.write(temp, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			try {
				if(os!=null){
					os.close();
				}
				if(is!=null){
					is.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值