Copy a file
来源:互联网 发布:大华设备搜索软件 编辑:程序博客网 时间:2024/05/21 10:47
http://www.javapractices.com/topic/TopicAction.do?Id=246
In JDK 7+, copying a file is a simple operation, involving a single call to File.copy.
Example
import java.io.IOException;import java.nio.file.CopyOption;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.nio.file.StandardCopyOption;/** JDK 7+. */public final class CopyFilesNew { public static void main(String... aArgs) throws IOException { Path FROM = Paths.get("C:\\Temp\\from.txt"); Path TO = Paths.get("C:\\Temp\\to.txt"); //overwrite existing file, if exists CopyOption[] options = new CopyOption[]{ StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES }; Files.copy(FROM, TO, options); }}
In older JDKs, however, copying a file involves a lot more code. It can be done either with FileChannels or with basic streams. The FileChanneltechnique is usually faster.
Here's an example showing both techniques.
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.channels.FileChannel;/** Copy files, using two techniques, FileChannels and streams. Using FileChannels is usually faster than using streams. JDK 6-.*/public final class CopyFiles { /* Change these settings before running this class. */ /** The file to be copied. */ public static final String INPUT_FILE = "C:\\TEMP\\cottage.jpg"; /** The name of the copy to be created by this class. If this file doesn't exist, it will be created, along with any needed parent directories. */ public static final String COPY_FILE_TO = "C:\\TEMP10\\cottage_2.jpg"; /** Run the example. */ public static void main(String... aArgs) throws IOException{ File source = new File(INPUT_FILE); File target = new File(COPY_FILE_TO); CopyFiles test = new CopyFiles(); test.copyWithChannels(source, target, false); //test.copyWithStreams(source, target, false); log("Done."); } /** This may fail for VERY large files. */ private void copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend) { log("Copying files with channels."); ensureTargetDirectoryExists(aTargetFile.getParentFile()); FileChannel inChannel = null; FileChannel outChannel = null; FileInputStream inStream = null; FileOutputStream outStream = null; try{ try { inStream = new FileInputStream(aSourceFile); inChannel = inStream.getChannel(); outStream = new FileOutputStream(aTargetFile, aAppend); outChannel = outStream.getChannel(); long bytesTransferred = 0; //defensive loop - there's usually only a single iteration : while(bytesTransferred < inChannel.size()){ bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel); } } finally { //being defensive about closing all channels and streams if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); if (inStream != null) inStream.close(); if (outStream != null) outStream.close(); } } catch (FileNotFoundException ex){ log("File not found: " + ex); } catch (IOException ex){ log(ex); } } private void copyWithStreams(File aSourceFile, File aTargetFile, boolean aAppend) { log("Copying files with streams."); ensureTargetDirectoryExists(aTargetFile.getParentFile()); InputStream inStream = null; OutputStream outStream = null; try{ try { byte[] bucket = new byte[32*1024]; inStream = new BufferedInputStream(new FileInputStream(aSourceFile)); outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, aAppend)); int bytesRead = 0; while(bytesRead != -1){ bytesRead = inStream.read(bucket); //-1, 0, or more if(bytesRead > 0){ outStream.write(bucket, 0, bytesRead); } } } finally { if (inStream != null) inStream.close(); if (outStream != null) outStream.close(); } } catch (FileNotFoundException ex){ log("File not found: " + ex); } catch (IOException ex){ log(ex); } } private void ensureTargetDirectoryExists(File aTargetDir){ if(!aTargetDir.exists()){ aTargetDir.mkdirs(); } } private static void log(Object aThing){ System.out.println(String.valueOf(aThing)); }}
0 0
- copy a single file
- Copy a file
- Copy a file
- copy the content of a file to another file.
- COPY FILE
- copy file
- Copy file
- copy file
- copy file
- copy file
- ssh远程copy文件报not a regular file错误
- How to copy a datafile from ASM to a file system not using RMAN
- Copy file with hole
- Copy txt file
- java nio copy file
- STAF FS COPY FILE
- py copy file
- C# File.Copy介绍
- 服务器端我们通过一个 Servlet 来处理来自客户端的请求,并对客户端作出响应。其核心处理代码
- Ubuntu 系统装机指南
- 小故事0送月亮回家
- XTUOJ-1058 青蛙王子的一种实现
- 2013华为机试题的个人解答c/c++
- Copy a file
- UVa 639 - Don't Get Rooked
- 搭建最简webservice,ruby+WEBrick+rack+grape
- ADT SDK下载问题
- C++中的函数指针和指针函数
- Reading and writing binary files
- [YARN] 编程模型-MR
- java获取硬件信息
- HTTP状态码