java 各种copy 博客分类: Java copy复制拷贝复制Listrange copy
程序员文章站
2024-03-26 11:24:53
...
java里面有许多需要copy的地方,比如copy文件,copy数组,现总结如下
(1)copy文件
public static void copyFile(String resourceFileName, String targetFileName) throws IOException { File resourceFile = new File(resourceFileName); File targetFile = new File(targetFileName); if (!resourceFile.exists()) { System.out.println("[copyFile ]: resource file has not been found:" + resourceFileName); } if (!resourceFile.isFile()) { System.out.println("[copyFile ]: directory can not be copyed:" + resourceFileName); } if (targetFile.isDirectory()) { targetFile = new File(targetFile, resourceFile.getName()); } FileInputStream resource = null; FileOutputStream target = null; try { resource = new FileInputStream(resourceFile); target = new FileOutputStream(targetFile); copyFile(resourceFile, targetFile); } catch (Exception e) { e.printStackTrace(); } finally { if (resource != null) { resource.close(); } if (target != null) { target.close(); } } } /** * * @param srcFile * :must be regular file,can not be folder; * @param targetFile * :must be regular file,can not be folder; * @throws IOException */ public static void copyFile(File srcFile, File targetFile) throws IOException { FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(targetFile); copyFile(in, out); } public static void copyFile(InputStream in, FileOutputStream target) throws IOException { // File targetFile = new File(targetFileName); // FileOutputStream target = null; // if (targetFile.isDirectory()) // { // targetFile = new File(targetFile, simpleName); // } try { // target = new FileOutputStream(targetFile); byte[] buffer = new byte[BUFF_SIZE]; int byteNum; while ((byteNum = in.read(buffer)) != NEGATIVE_ONE) { target.write(buffer, 0, byteNum); } System.out.println("[SystemUtil:copyFile]:file copy successfully!"); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { in.close(); in = null; } if (target != null) { target.close(); target = null; } } }
(2)copy数组
/*** * 合并字节数组 * * @param a * @return */ public static byte[] mergeArray(byte[]... a) { // 合并完之后数组的总长度 int index = 0; int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i].length; } byte[] result = new byte[sum]; for (int i = 0; i < a.length; i++) { int lengthOne = a[i].length; if (lengthOne == 0) { continue; } // 拷贝数组 System.arraycopy(a[i], 0, result, index, lengthOne); index = index + lengthOne; } return result; } /*** * append a byte. * * @param a * @param b * @return */ public static byte[] appandByte(byte[] a, byte b) { int length = a.length; byte[] resultBytes = new byte[length + 1]; System.arraycopy(a, 0, resultBytes, 0, length); resultBytes[length] = b; return resultBytes; } public static byte[] copyByte(int start, int length, byte[] source) { byte[] des = new byte[length]; System.arraycopy(source, start, des, 0, length); return des; }
(3)copy List
/*** * 复制 list * @param srcList * @param start * @param length * @return */ public static List copyList(List srcList,int start,int length){ if(ValueWidget.isNullOrEmpty(srcList)){ return null; } List resultList=new ArrayList(); for(int i=start;i<length+start&& i<srcList.size();i++){ resultList.add(srcList.get(i)); } return resultList; }
测试:
@Test public void test_ArrayList_remove(){ List list=new ArrayList<String>(); int range=3; list.add("aa"); list.add("bb"); list.add("cc"); list.add("dd"); list.add("ee"); list.add("ff"); // if(list.size()>range){//excel前两行是标题 // System.out.println("移除序号"+range+"...."); // for(int j=range;j<=list.size()+1;j++){ // Object obj32=list.remove(range); // } // } System.out.println( SystemHWUtil. copyList(list, 0, 14)); }
参考:
http://www.baidu.com/link?url=Wwh8KqbUeOaaKFCcp8xAQ8ALHUuunhm1kR95EAbhk20LkxkL_8pQbFCfnaoKqP1KX4pzf8lCeRWWFk2fEDqK5a
http://www.yunmasoft.com
上一篇: C++知识点总结1 基础知识点
下一篇: C++构造函数相关……