您的位置:首页 > 职场人生

黑马程序员——java-IO流中复制单击目录以及使用递归的方式复制多级目录小结

2015-02-03 16:10 597 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

文件的复制大概分为五种方式,如果是文本文档可以使用字节流复制,也可以使用字符流复制

如果是复制目录,由于目录中可以存放各种类型的文件,所以最好使用字节流的方式,因为字节流可以操作包括图片、语音、视屏等各种类型的文件进行复制

一、如果是单级目录:

1.首先创建文件目录

2.遍历出源文件内容

3.创建新的文件用于接收原文件内容

4.将源文件复制到新的文件中

5.关闭流资源

<span style="font-size:12px;">public class Copy {
	public static void main(String[] args) throws IOException {
		method2("a", "b");
	}

	//给定两个目录,会把第一个目录的内容复制到第二个目录下
	public static void method2(String src,String dest )throws FileNotFoundException, IOException {		
		File srcDIR = new File(src);
		File destDIR = new File(dest);
		File[] listFiles = srcDIR.listFiles();
		for (File file : listFiles) {//file:老文件夹下的遍历到的这个文件
			
<span style="white-space:pre">			</span>//创建一个新的文件,但是里边还没有内容
			File newFile = new File(destDIR,file.getName());
				method(file, newFile);
		}
	}

	//给定两个文件,会把第一个文件的内容复制到第二个文件中
	public static void method(File file, File newFile)throws FileNotFoundException, IOException {

		//创建字节流
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));

		//进行文件内容的复制
		byte[] bytes = new byte[1024];
		int len;
		while ((len=bis.read(bytes))!=-1) {
			bos.write(bytes,0,len);
		}

	       //关闭流资源	
		bos.close();
		bis.close();
	}

}</span>
二、使用递归的思想进行多级目录的复制

<span style="font-size:12px;">public class Copy {
	public static void main(String[] args) throws IOException {	

		File srcDIR = new File("D:\\Lesson..."); //给定要被复制的多级目录
		File destDIR = new File("a");		//复制到的目标文件
		method(srcDIR,destDIR);
	}
	/*
	 * src:被复制的文件对象
	 * destDIR:目标目录
	 */
	public static void method(File src,File destDIR) throws IOException {	

		if(!destDIR.exists()) { //如果目标文件目录不存在,则创建目标目录

			destDIR.mkdir();// 由于目录不能直接被创建,所以此处必须要手动创建目标目录
		}	

		if(src.isDirectory()) {//如果是目录	
			File[] listFiles = src.listFiles(); //返回目录下所有文件的数组
			File newDIR = new File(destDIR,src.getName()); //创建新的目录对象
			newDIR.mkdir();	//创建新的目录

			for (File file : listFiles) {  //遍历目录下所有文件
				method(file,newDIR);  // 递归调用复制多级目录的方法
			}	
		} else { //如果是文件	
			//创建目标文件
			File newFile = new File(destDIR,src.getName());	
			//将形参src(被复制的文件)复制到目标文件中
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));	
			byte[] bytes = new byte[1024];
			int len;
			while((len=bis.read(bytes))!=-1) {
				bos.write(bytes,0,len);
<span style="white-space:pre">				</span></span><pre name="code" class="java"><span style="font-size:12px;">	<span style="white-space:pre">			</span>bos.flush();
		     }	
			bos.close();
			bis.close();
		}
	}
}</span>



总结:多级目录的复制由于使用的是字节流,所以可以复制任何类型的文件,同时也可以进行单级目录的复制,通过不断的向上抽取封装,可以是复制文件看起来更

简单,只需给定两个文件的字符串路径即可,我想这就是面向对象思想的体现吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐