博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO练习之拷贝文件夹
阅读量:3959 次
发布时间:2019-05-24

本文共 1383 字,大约阅读时间需要 4 分钟。

题目

键盘录入一个文件夹路径,作为源文件夹;键盘录入一个文件夹路径,作为目标文件夹

写代码将源文件夹拷贝到目标文件夹中
a
b
b/a

 

代码实现

public class demo {		public static void change(File file1,String first1,String first2) throws IOException {				//创建文件夹		String s1 = file1.getAbsolutePath().replace(first1,"");		String s2 = first2;				String end = s2+"\\"+s1;		File file = new File(end);		file.mkdirs();				//获得文件夹下内容		File[] files = file1.listFiles();				for(File f:files) {			if(f.isFile()) {								String ss1 = f.getAbsolutePath().replace(first1,"");				String ss2 = first2;								String file_end = ss2+"\\"+ss1;				File ff = new File(file_end);				ff.createNewFile();								BufferedInputStream b = new BufferedInputStream(new FileInputStream(f));				BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(ff));				int len = 0;				byte[] bt = new byte[1024]; 				while((len = b.read(bt))!=-1) {					o.write(bt,0,len);				}				b.close();				o.close();			}else {				change(f,first1, first2);			}		}		}		public static void main(String[] args) throws IOException {				Scanner scanner = new Scanner(System.in);		String s1 = scanner.nextLine();		String s2 = scanner.nextLine();				File file1 = new File(s1);//"D:/111"		File file2 = new File(s1);//"D:/123"		String first1 = file1.getAbsolutePath().split(file1.getName())[0];		String first2 = file2.getAbsolutePath();				change(file1,first1,first2);		System.out.println("拷贝完毕");	}}

 

转载地址:http://dpazi.baihongyu.com/

你可能感兴趣的文章
CImg库编译使用.
查看>>
Android性能优化
查看>>
Canvas入门(一)
查看>>
Canvas入门(一)
查看>>
Canvas入门(一)
查看>>
DbUtils入门
查看>>
DTD约束简介
查看>>
Eclipse Debug调试
查看>>
Eclipse Debug调试
查看>>
Hibernate的注解和XML
查看>>
JavaScript闭包
查看>>
JavaScript原型
查看>>
JavaScript原型
查看>>
JavaScript原型
查看>>
JDK工具
查看>>
JDK工具
查看>>
JNA-JNI升级版
查看>>
JNA-JNI升级版
查看>>
Android 下 JNI 开发
查看>>
Android 下 JNI 开发
查看>>