黑马程序员技术交流社区

标题: 用数组操作拷贝文件 [打印本页]

作者: 凡沉香    时间: 2016-4-10 21:11
标题: 用数组操作拷贝文件
具体代码怎么写?
作者: lmr1096200234    时间: 2016-4-10 22:02
数组的复制方法现在至少有四个思路:

  1 使用循环结构 这种方法最灵活。唯一不足的地方可能就是代码较多

  2 使用Object类的clone()方法, 这种方法最简单,得到原数组的一个副本。灵活形也最差。效率最差,尤其是在数组元素很大或者复制对象数组时。

  3 使用Systems的arraycopy这种方法被告之速度最快,并且灵活性也较好,可以指定原数组名称、以及元素的开始位置、复制的元素的个数,目标数组名称、目标数组的位置。

  4 Arrarys类的copyOf()方法与copyOfRange()方法可实现对数组的复制
作者: 可可TKD    时间: 2016-4-10 22:05
第一种拷贝方式:通过缓冲流和字节数组相结合
         * @param src 源文件
         * @param dest 目标文件
         */
        public static boolean copy1(String src, String dest) {
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                boolean flag = false;
               
                try {
                        bis = new BufferedInputStream(new FileInputStream(src));
                        bos = new BufferedOutputStream(new FileOutputStream(dest));
                                       
                        byte[] data = new byte[1024*10];
                        int len;//向data数组中写入了几个数据
                       
                        while((len=bis.read(data)) != -1){
                                bos.write(data,0,len);
                        }
                        flag = true;
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }finally{
                        if(bis != null){
                                try {
                                        bis.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                        if(bos != null){
                                try {
                                        bos.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }
               
               
                return flag;
        }
       
        /**
         * 通过缓冲流拷贝,不采用小数组
         * @param src
         * @param dest
         * @return
         */
        public static boolean copy2(String src, String dest){
                boolean flag = false;//标记我程序是否成功拷贝
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                        bis = new BufferedInputStream(new FileInputStream(src));
                        bos = new BufferedOutputStream(new FileOutputStream(dest));
                       
                        int data;//每次读取到的数据
                        while((data = bis.read()) != -1){
                                bos.write(data);
                        }
                        flag = true;
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch(Exception e){
                        e.printStackTrace();
                }finally{
                        if(bis != null){
                                try {
                                        bis.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                        if(bos != null){
                                try {
                                        bos.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
               
                return flag;
        }
       
        /**
         * 采用普通字节输入输出流+小数组
         * @param src
         * @param dest
         * @return
         */
        public static boolean copy3(String src, String dest){
                FileInputStream fis = null;
                FileOutputStream fos = null;
                boolean flag = false;
                try {
                        fis = new FileInputStream(src);
                        fos = new FileOutputStream(dest);
                        byte[] data = new byte[1024*10];
                        int len;
                       
                        while((len=fis.read(data)) != -1){
                                fos.write(data, 0, len);
                        }
                        flag = true;
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch(Exception e){
                        e.printStackTrace();
                }finally{
                        if(fis != null){
                                try {
                                        fis.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                        if(fos != null){
                                try {
                                        fos.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
               
                return flag;
        }




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