黑马程序员技术交流社区

标题: 看到这个题直接一脸蒙蔽. [打印本页]

作者: z443863517    时间: 2016-10-15 21:30
标题: 看到这个题直接一脸蒙蔽.
题目三:把指定目录下(包含子目录)的所有图片,复制到另一个指定目录下


作者: 冷猫    时间: 2016-10-15 21:34
一脸懵逼的进来,一脸懵逼的离开
作者: 灵活的胖子    时间: 2016-10-16 09:27
是用命令行指令吧,这也要考?

作者: 哦啊啊    时间: 2016-10-16 09:36
这不是io的题吗,命令行?不至于吧

作者: z443863517    时间: 2016-10-16 11:31
哦啊啊 发表于 2016-10-16 09:36
这不是io的题吗,命令行?不至于吧

就是io的题目呀

作者: 细听风语为梧桐    时间: 2016-10-16 11:55
这个感觉得先知道图片的格式有哪些,然后按照后缀名进行io复制
作者: barny    时间: 2016-10-16 12:04
这个很简单啊,递归搞定,做了很多类似
作者: hysnxdss    时间: 2016-10-16 12:25
楼顶正解,递归和IO结合,,,,判断是文件就IO复制,是文件夹就继续掉用自己
作者: IceLoveInFire丶    时间: 2016-10-16 13:18
多级文件夹复制,加个判断是图片就可以了吧?递归

作者: 蓝光四号    时间: 2016-10-16 14:30
还没学11111
作者: yinyujue    时间: 2016-10-16 15:38
递归获得所有图片,然后复制到目的文件夹中
作者: 韩可可    时间: 2016-10-16 18:35
java基础班课程有案例的一模一样 你别说你没看到
作者: 李孝志    时间: 2016-10-16 21:42
下面的代码应该可以

[Java] 纯文本查看 复制代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test3 {
        String imageReg = "^[a-zA-Z0-9_]+(\\.jpeg|\\.gif|\\.png|\\.bmp|\\.tiff|\\.psd|\\.svg|\\.jpg)$";
        public static void main(String[] args) {
                Test3 test = new Test3();
                test.copyimage("d://aaa");
        }
       
       
        public void copyimage(String path) {
                File srcFile = new File(path);
                String[] file = srcFile.list();
                for (int i = 0; i < file.length; i++) {
                        File temp = new File(path + "/" + file);
                        System.out.println(path + "/" + file);
                        if (temp.isFile()) {
                                if (temp.getName().matches(imageReg))                                {
                                        String result = fileCopy(path + "/" + file, "D://a_cope");
                                        System.out.println(result);
                                }
                        }
                        else
                        {
                                if (temp.isDirectory()) {
                                        copyimage(path + "/" + file);
                                        System.out.println("我又执行一遍");
                                }
                        }
                }
        }

        public static String fileCopy(String srcPath, String desPath) {
                String result = "";
                File old = new File(srcPath);
                File newImage = new File(desPath);
                newImage.mkdirs();
                FileInputStream fis = null;
                FileOutputStream fos = null;
                try {
                        if (!old.exists()) {
                                result = "the file is not exists";
                                return result;
                        } else {
                                fis = new FileInputStream(old);
                                if (!newImage.exists()) {
                                        // newImage.mkdir();
                                        newImage.createNewFile();
                                }
                                fos = new FileOutputStream(newImage + "/" + old.getName());
                                byte[] temp = new byte[1000];
                                int size = fis.read(temp);
                                while (size != -1) {
                                        fos.write(temp);
                                        size = fis.read(temp);
                                }
                                result = "the File Copy is success!";
                                return result;
                        }
                } catch (FileNotFoundException fileNot) {
                        result = "the File Copy is Failed!";
                        fileNot.printStackTrace();
                        return result;
                } catch (IOException e) {
                        e.printStackTrace();
                        result = "the File Copy is Failed!";
                        return result;
                } finally {
                        try {
                                fis.close();
                                fos.close();
                        } catch (Exception ex) {
                        }
                }
        }
       
}

作者: wangkai426    时间: 2016-10-16 22:00
难道是传说中的命令行?




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2