java实现文件或文件夹赋值、剪切

java中将文件复制到另一个路径,实际上就是通过io流把文件读取到另一路径下。而剪切则是复制完成后删除原文件,可用以下代码实现。

<pre name="code" class="java">/**
 * @param origPath 需复制的文件路径
 * @param newPath  复制到相应位置的文件路径
 * @description 复制文件
 */
public static boolean copyFile(String origPath, String newPath){
	File o = new File(origPath);
	try{
		if(o.isFile()){
			copy(o, newPath,false);
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	return false;
}

/**
 * 
 * @param origPath 需复制的文件夹名
 * @param newPath 复制到文件夹名
 * @return boolean 是否复制成功
 * @description 复制文件夹
 */
public static boolean copyDirectiory(String origPath, String newPath){
	File o = new File(origPath);
	try{
		if(o.isDirectory()){
			copy(o, newPath,false);
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	return false;
}


/**
 * @param origPath 需剪切的文件路径
 * @param newPath 剪切到相应位置的文件路径
 * @description 剪切文件
 */
public static boolean cutFile(String origPath, String newPath){
	File o = new File(origPath);
	try{
		if(o.isFile()){
			copy(o, newPath,true);
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	return false;
}

/**
 * 
 * @param origPath 剪切制的文件夹名
 * @param newPath 剪切到文件夹名
 * @return boolean 剪切复制成功
 * @description 剪切文件夹
 */
public static boolean cutDirectiory(String origPath, String newPath){
	File o = new File(origPath);
	try{
		if(o.isDirectory()){
			copy(o, newPath,true);
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	return false;
}

/**
 * @param origPath 需复或剪切制的文件File对象
 * @param newPath 需复或剪切到路径
 * @param isCut 是否剪切
 * @throws IOException
 * @description 需复或剪切
 */
public static void copy(File o, String newPath,boolean isCut) throws IOException{
	InputStream is = null;
	OutputStream os = null;
	try{
		File n = new File(newPath);
		File p = n.getParentFile();
		//复制位置父目录是否存在,不存在则创建
		if(!p.exists())
			p.mkdirs();
		//复制对象为文件夹
		if(o.isDirectory()){
			if(!n.exists())
				n.mkdir();
			//获取文件下所有子对象
			File[] files = o.listFiles();
			for (File file : files) {
				String path = newPath;
				if(!path.endsWith(File.separator))
					path += File.separator;
				path += file.getName();
				copy(file, path,isCut);
			}
		}
		//复制对象为文件
		else{
			if(!n.exists())
				n.createNewFile();
			is = new FileInputStream(o);
			os = new FileOutputStream(n);
			byte[] b = new byte[1024];
			while(is.read(b) != -1){
				os.write(b);
			}
			os.flush();
		}
	}finally{
		try {
			if(is != null){
				is.close();
			}
			if(os != null){
				os.close();
			}
			//若isCut为true,复制完成对象后删除原对象
			if(isCut){
				o.delete();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

/**
 * @param origPath 需复或剪切制的文件路径
 * @param newPath 需复或剪切到路径
 * @param isCut 是否剪切
 * @throws IOException
 * @description 需复或剪切
 */
public static void copy(String origPath, String newPath,boolean isCut) throws IOException{
	InputStream is = null;
	OutputStream os = null;
	File o = new File(origPath);
	try{
		File n = new File(newPath);
		File p = n.getParentFile();
		//复制位置父目录是否存在,不存在则创建
		if(!p.exists())
			p.mkdirs();
		//复制对象为文件夹
		if(o.isDirectory()){
			if(!n.exists())
				n.mkdir();
			//获取文件下所有子对象
			File[] files = o.listFiles();
			for (File file : files) {
				String path = newPath;
				if(!path.endsWith(File.separator))
					path += File.separator;
				path += file.getName();
				copy(file.getPath(), path,isCut);
			}
		}
		//复制对象为文件
		else{
			if(!n.exists())
				n.createNewFile();
			is = new FileInputStream(o);
			os = new FileOutputStream(n);
			byte[] b = new byte[1024];
			while(is.read(b) != -1){
				os.write(b);
			}
			os.flush();
		}
		//若isCut为true,复制完成对象后删除原对象
		if(isCut)
			o.delete();
	}finally{
		try {
			if(is != null){
				is.close();
			}
			if(os != null){
				os.close();
			}
			//若isCut为true,复制完成对象后删除原对象
			if(isCut){
				o.delete();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 

以上代码实现多种方式复制或剪切文件或文件夹。


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值