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 介绍 续篇,该专栏会持续更新,感兴趣的朋友请订阅我们。
5856 0
|
缓存 Java Apache
Java Bean Copy框架性能对比
一、问题分析 背景 相同server机器上的相同方法在方法调用链任何参数都一致的情况消耗时间差别非常大,举例说明,类A有方法demo(), 通过分析发现同一台机器(也是一个jvm进程)对该方法的两次调用消耗时间竟然有200ms的差距。
12749 0
|
Java
java中copy 一个list集合的方法
java将一个list里的数据转移到另外一个list,可以使用for语句,一次使用add方法,示例如下:   ArrayList list1=new ArrayList(); list1.add("1"); list1.
2965 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
|
9天前
|
存储 Java 数据库连接
java多线程之线程通信
java多线程之线程通信
|
9天前
|
安全 Java 开发者
深入理解Java并发编程:线程安全与性能优化
【4月更文挑战第9天】本文将深入探讨Java并发编程的核心概念,包括线程安全和性能优化。我们将详细解析Java中的同步机制,包括synchronized关键字、Lock接口以及并发集合等,并探讨它们如何影响程序的性能。此外,我们还将讨论Java内存模型,以及它如何影响并发程序的行为。最后,我们将提供一些实用的并发编程技巧和最佳实践,帮助开发者编写出既线程安全又高效的Java程序。
22 3
|
8天前
|
算法 Java 开发者
Java中的多线程编程:概念、实现与性能优化
【4月更文挑战第9天】在Java编程中,多线程是一种强大的工具,它允许开发者创建并发执行的程序,提高系统的响应性和吞吐量。本文将深入探讨Java多线程的核心概念,包括线程的生命周期、线程同步机制以及线程池的使用。接着,我们将展示如何通过继承Thread类和实现Runnable接口来创建线程,并讨论各自的优缺点。此外,文章还将介绍高级主题,如死锁的预防、避免和检测,以及如何使用并发集合和原子变量来提高多线程程序的性能和安全性。最后,我们将提供一些实用的性能优化技巧,帮助开发者编写出更高效、更稳定的多线程应用程序。