本文共 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/