利用FileChannel复制文件 Copy one File to Another【三种方法】
2009-05-22 13:40
309 查看
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; /** * 复制文件 * @author Administrator * */ public class CopyFile { public static void main(String[] args) { try { // Create channel on the source FileChannel srcChannel = new FileInputStream("D:\\My Documents\\08-100附件二广州社保培训资料.doc") .getChannel(); // Create channel on the destination FileChannel dstChannel = new FileOutputStream("D:\\My Documents\\dstFilename.doc") .getChannel(); // Copy file contents from source to destination dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); // Close the channels srcChannel.close(); dstChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } 再添两种方法: import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; public class FileCopyTest { public static void main(String[] args) { //方法一 try { FileReader fr = new FileReader("c:\\updatedatfix.log"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("c:\\temp2.txt"); PrintWriter pw = new PrintWriter(fw, true); String line; while ((line = br.readLine()) != null) { pw.println(line); } } catch (Exception e) { e.printStackTrace(); } //方法二 try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\updatedatfix.log")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\tt.log")); byte[] buff = new byte[1024]; int len =0; while((len=bis.read(buff,0,1024))!=-1){ bos.write(buff, 0, len); } bis.close(); bos.close(); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- 4 Ways to Copy File in Java -- Java 复制文件的方法
- ubuntu 复制文件到xp (How i can copy file from server(OS:ubuntu 10.04) to my PC
- 利用 hadoop 的 FileSystem copyFromLocalFile 方法实现文件上传HDFS中 Hadoop 版本 2.7.0
- FileChannel-Java 复制文件的高效方法
- java 文件移动 常规四种方法 和利用FileCopyUtils.copy( , )方法
- 利用spring中的FileCopyUtils.copy()复制文件
- 利用FileChannel完成文件的读、写、复制
- 利用spring中的FileCopyUtils.copy()复制文件
- 利用FileChannel复制文件
- java 文件移动 常规四种方法 和利用FileCopyUtils.copy( , )方法
- Java NIO——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- 利用spring中的FileCopyUtils.copy()复制文件
- 利用 hadoop 的 FileSystem copyFromLocalFile 方法实现文件上传HDFS中 Hadoop 版本 2.7.0
- 利用FileChannel完成文件的读、写、复制
- Java NIO——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- 利用 Hadoop FileSystem moveToLocalFile 方法下载文件 实现HDFS操作
- asp.net 文件复制或删除用相对路径,File.Copy中用相对路径,巧用相对路径复制文件
- Java IO学习【13】利用字节流复制文件的4种方法详解
- Eclipse导入Android文件出错!Project has no default.properties file! Edit the project properties to set one
- PowerDesigner打开设计文件后提示failed to read the fileXXX的解决方法