A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 凡沉香 中级黑马   /  2016-4-10 21:11  /  344 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

具体代码怎么写?

2 个回复

倒序浏览
数组的复制方法现在至少有四个思路:

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

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

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

  4 Arrarys类的copyOf()方法与copyOfRange()方法可实现对数组的复制
回复 使用道具 举报
第一种拷贝方式:通过缓冲流和字节数组相结合
         * @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;
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马