java 各种copy

简介:

Java里面有许多需要copy的地方,比如copy文件,copy数组,现总结如下

(1)copy文件

Java代码   收藏代码
  1. public static void copyFile(String resourceFileName, String targetFileName)  
  2.             throws IOException {  
  3.         File resourceFile = new File(resourceFileName);  
  4.         File targetFile = new File(targetFileName);  
  5.         if (!resourceFile.exists()) {  
  6.             System.out.println("[copyFile ]: resource file has not been found:"  
  7.                     + resourceFileName);  
  8.         }  
  9.         if (!resourceFile.isFile()) {  
  10.             System.out.println("[copyFile ]: directory can not be copyed:"  
  11.                     + resourceFileName);  
  12.         }  
  13.   
  14.         if (targetFile.isDirectory()) {  
  15.             targetFile = new File(targetFile, resourceFile.getName());  
  16.         }  
  17.   
  18.         FileInputStream resource = null;  
  19.         FileOutputStream target = null;  
  20.         try {  
  21.             resource = new FileInputStream(resourceFile);  
  22.             target = new FileOutputStream(targetFile);  
  23.             copyFile(resourceFile, targetFile);  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.         } finally {  
  27.             if (resource != null) {  
  28.                 resource.close();  
  29.             }  
  30.             if (target != null) {  
  31.                 target.close();  
  32.             }  
  33.         }  
  34.     }  
  35.   
  36. /** 
  37.      *  
  38.      * @param srcFile 
  39.      *            :must be regular file,can not be folder; 
  40.      * @param targetFile 
  41.      *            :must be regular file,can not be folder; 
  42.      * @throws IOException 
  43.      */  
  44.     public static void copyFile(File srcFile, File targetFile)  
  45.             throws IOException {  
  46.         FileInputStream in = new FileInputStream(srcFile);  
  47.   
  48.         FileOutputStream out = new FileOutputStream(targetFile);  
  49.         copyFile(in, out);  
  50.   
  51.     }  
  52. public static void copyFile(InputStream in, FileOutputStream target)  
  53.             throws IOException {  
  54.         // File targetFile = new File(targetFileName);  
  55.         // FileOutputStream target = null;  
  56.         // if (targetFile.isDirectory())  
  57.         // {  
  58.         // targetFile = new File(targetFile, simpleName);  
  59.         // }  
  60.         try {  
  61.             // target = new FileOutputStream(targetFile);  
  62.             byte[] buffer = new byte[BUFF_SIZE];  
  63.             int byteNum;  
  64.   
  65.             while ((byteNum = in.read(buffer)) != NEGATIVE_ONE) {  
  66.                 target.write(buffer, 0, byteNum);  
  67.   
  68.             }  
  69.             System.out.println("[SystemUtil:copyFile]:file copy successfully!");  
  70.         } catch (Exception e) {  
  71.             e.printStackTrace();  
  72.         } finally {  
  73.             if (in != null) {  
  74.                 in.close();  
  75.                 in = null;  
  76.             }  
  77.             if (target != null) {  
  78.                 target.close();  
  79.                 target = null;  
  80.             }  
  81.         }  
  82.     }  

 

(2)copy数组

Java代码   收藏代码
  1. /*** 
  2.      * 合并字节数组 
  3.      *  
  4.      * @param a 
  5.      * @return 
  6.      */  
  7.     public static byte[] mergeArray(byte[]... a) {  
  8.         // 合并完之后数组的总长度  
  9.         int index = 0;  
  10.         int sum = 0;  
  11.         for (int i = 0; i < a.length; i++) {  
  12.             sum = sum + a[i].length;  
  13.         }  
  14.         byte[] result = new byte[sum];  
  15.         for (int i = 0; i < a.length; i++) {  
  16.             int lengthOne = a[i].length;  
  17.             if (lengthOne == 0) {  
  18.                 continue;  
  19.             }  
  20.             // 拷贝数组  
  21.             System.arraycopy(a[i], 0, result, index, lengthOne);  
  22.             index = index + lengthOne;  
  23.         }  
  24.         return result;  
  25.     }  
  26. /*** 
  27.      * append a byte. 
  28.      *  
  29.      * @param a 
  30.      * @param b 
  31.      * @return 
  32.      */  
  33.     public static byte[] appandByte(byte[] a, byte b) {  
  34.         int length = a.length;  
  35.         byte[] resultBytes = new byte[length + 1];  
  36.         System.arraycopy(a, 0, resultBytes, 0, length);  
  37.         resultBytes[length] = b;  
  38.         return resultBytes;  
  39.     }  
  40. public static byte[] copyByte(int start, int length, byte[] source) {  
  41.         byte[] des = new byte[length];  
  42.         System.arraycopy(source, start, des, 0, length);  
  43.         return des;  
  44.     }  

 

(3)copy List

Java代码   收藏代码
  1. /*** 
  2.      * 复制 list 
  3.      * @param srcList 
  4.      * @param start 
  5.      * @param length 
  6.      * @return 
  7.      */  
  8.     public static List copyList(List srcList,int start,int length){  
  9.         if(ValueWidget.isNullOrEmpty(srcList)){  
  10.             return null;  
  11.         }  
  12.         List resultList=new ArrayList();  
  13.         for(int i=start;i<length+start&& i<srcList.size();i++){  
  14.             resultList.add(srcList.get(i));  
  15.         }  
  16.         return resultList;  
  17.     }  

 测试:

Java代码   收藏代码
  1. @Test  
  2.     public void test_ArrayList_remove(){  
  3.         List list=new ArrayList<String>();  
  4.         int range=3;  
  5.         list.add("aa");  
  6.         list.add("bb");  
  7.         list.add("cc");  
  8.         list.add("dd");  
  9.         list.add("ee");  
  10.         list.add("ff");  
  11. //      if(list.size()>range){//excel前两行是标题  
  12. //          System.out.println("移除序号"+range+"....");  
  13. //          for(int j=range;j<=list.size()+1;j++){  
  14. //              Object obj32=list.remove(range);  
  15. //          }  
  16. //      }  
  17.         System.out.println( SystemHWUtil. copyList(list, 014));  
  18.     }  

 参考:

http://www.baidu.com/link?url=Wwh8KqbUeOaaKFCcp8xAQ8ALHUuunhm1kR95EAbhk20LkxkL_8pQbFCfnaoKqP1KX4pzf8lCeRWWFk2fEDqK5a

http://www.yunmasoft.com

相关文章
|
Java Spring 微服务
Java Bean Copy 性能大比拼
Java Bean Copy 性能大比拼 简介 Bean 拷贝在工作中被大量使用,可以大幅度的提高工作量。本文对常用的 Bean copy 工具进行了压力测试,方便大家选择更加适合自己的工具。本篇文章是mica cglib 增强——【01】cglib bean copy 介绍 续篇,该专栏会持续更新,感兴趣的朋友请订阅我们。
5867 0
|
缓存 Java Apache
Java Bean Copy框架性能对比
一、问题分析 背景 相同server机器上的相同方法在方法调用链任何参数都一致的情况消耗时间差别非常大,举例说明,类A有方法demo(), 通过分析发现同一台机器(也是一个jvm进程)对该方法的两次调用消耗时间竟然有200ms的差距。
12839 50
|
Java
java中copy 一个list集合的方法
java将一个list里的数据转移到另外一个list,可以使用for语句,一次使用add方法,示例如下:   ArrayList list1=new ArrayList(); list1.add("1"); list1.
2968 0
|
Java
【Util】java copy bean 反射
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
670 0
|
Java
java移动/赋值文件 copy/move file
1 public class FileAccess 2 { 3 4 public static boolean Move(File srcFile, String destPath) 5 { 6 // Destination directory ...
579 0
|
22小时前
|
缓存 Java 调度
Java并发编程:深入理解线程池
【4月更文挑战第30天】 在Java并发编程中,线程池是一种重要的工具,它可以帮助我们有效地管理线程,提高系统性能。本文将深入探讨Java线程池的工作原理,如何使用它,以及如何根据实际需求选择合适的线程池策略。
|
1天前
|
Java
Java并发编程:深入理解线程池
【4月更文挑战第30天】 本文将深入探讨Java中的线程池,解析其原理、使用场景以及如何合理地利用线程池提高程序性能。我们将从线程池的基本概念出发,介绍其内部工作机制,然后通过实例演示如何创建和使用线程池。最后,我们将讨论线程池的优缺点以及在实际应用中需要注意的问题。
|
1天前
|
设计模式 算法 安全
Java多线程编程实战:从入门到精通
【4月更文挑战第30天】本文介绍了Java多线程编程的基础,包括线程概念、创建线程(继承`Thread`或实现`Runnable`)、线程生命周期。还讨论了线程同步与锁(同步代码块、`ReentrantLock`)、线程间通信(等待/通知、并发集合)以及实战技巧,如使用线程池、线程安全设计模式和避免死锁。性能优化方面,建议减少锁粒度和使用非阻塞算法。理解这些概念和技术对于编写高效、可靠的多线程程序至关重要。