Java 杂记
Posted 小鱼等鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 杂记相关的知识,希望对你有一定的参考价值。
文件拷贝两种方式
//不断内存拷贝,大文件增加cpu负担 public static void copyFile(String srcFile, String destFile) throws Exception { byte[] temp = new byte[1024]; FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); int length; while ((length = in.read(temp)) != -1) { out.write(temp, 0, length); } in.close(); out.close(); } //减少内存拷贝 public static void copyFileWithFileChannel(String srcFileName, String destFileName) throws Exception { RandomAccessFile srcFile = new RandomAccessFile(srcFileName, "r"); FileChannel srcFileChannel = srcFile.getChannel(); RandomAccessFile destFile = new RandomAccessFile(destFileName, "rw"); FileChannel destFileChannel = destFile.getChannel(); long position = 0; long count = srcFileChannel.size(); srcFileChannel.transferTo(position, count, destFileChannel); }
以上是关于Java 杂记的主要内容,如果未能解决你的问题,请参考以下文章