java处理文件的复制

Posted letmeknow

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java处理文件的复制相关的知识,希望对你有一定的参考价值。

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileCopy
{
public static void copyFileUsingFileChannels(String sourcePath, String destPath) throws IOException {
File source=new File(sourcePath);
File dest=new File(destPath);
dest.setExecutable(true);//设置可执行权限(发布在tomcat时若权限不足,没法进行操作则设置权限)
dest.setReadable(true);//设置可读权限
dest.setWritable(true);//设置可写权限

FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
}

 






















以上是关于java处理文件的复制的主要内容,如果未能解决你的问题,请参考以下文章