/**
* 支持jdk7以上
* @param source
* @param target
*/
public static void copyFileJ(String source,String target) {
File fileFile=new File(source);
if (!fileFile.isFile()) {
return;
}
File targetFile=new File(target);
try {
Files.copy(fileFile.toPath(), targetFile.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 基于BIO传统的文件拷贝
* @param source
* @param target
*/
public static void copyBio(String source,String target) {
File sourceFile=new File(source);
File targetFile=new File(target);
copyFileBio(sourceFile, targetFile);
}
public static void copyFileBio(File sourceFile,File targetFile) {
try {
BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(targetFile));
byte[] bs=new byte[1024];
int count;
while((count=inputStream.read(bs))!=-1) {
outputStream.write(bs, 0, count);
}
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 基于Nio的文件拷贝 每次拷贝文件的一小块
* @param source
* @param target
*/
public static void copyNioBuffer(String source,String target) {
try {
RandomAccessFile sourceFile=new RandomAccessFile(source, "rw");
FileChannel sourceChannel=sourceFile.getChannel();
RandomAccessFile targetFile=new RandomAccessFile(target, "rw");
FileChannel targetChannl=targetFile.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(1024);
int byteRead=sourceChannel.read(buffer);
while(byteRead != -1) {
buffer.flip();
targetChannl.write(buffer);
buffer.compact();
byteRead=sourceChannel.read(buffer);
}
targetChannl.close();
sourceChannel.close();
targetFile.close();
sourceFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void copyNio(String source,String target) {
try {
RandomAccessFile sourceFile=new RandomAccessFile(source, "rw");
FileChannel sourceChannel=sourceFile.getChannel();
RandomAccessFile targetFile=new RandomAccessFile(target, "rw");
FileChannel targetChannel=targetFile.getChannel();
long position=0;
long count=sourceChannel.size();
targetChannel.transferFrom(sourceChannel, position, count);
sourceChannel.close();
targetChannel.close();
sourceFile.close();
targetFile.close();
} catch ( IOException e) {
e.printStackTrace();
}
}
/**
* 文件夹的拷贝
* @param source
* @param target
*/
public static void copyDir(String source,String target) {
if (!target.endsWith(File.separator)) {
target+=File.separator;
}
File file=new File(source);
if (file.isDirectory()) {
recursionDir(file,target);
}else {
target+=file.getName();
File targetFile=new File(target);
copyFileBio(file, targetFile);
}
}
private static void recursionDir(File fileSource,String base) {
if (fileSource.isDirectory()) {
File[] files=fileSource.listFiles();
if (files.length>0) {
for(File file:files) {
String basetemp=base+File.separator+file.getName();
recursionDir(file,basetemp);
}
}
}else {
File targetFile=new File(base);
File fileParent=targetFile.getParentFile();
if (!fileParent.exists()) {
fileParent.mkdirs();
}
copyFileBio(fileSource, targetFile);
}
}
/**
* 文件以及文件夹的删除
* @param file
*/
public static void deleteFileDir(File file) {
if (file.exists()) {
if (file.isDirectory()) {
File[] files=file.listFiles();
for(File f:files) {
deleteFileDir(f);
}
}
file.delete();
}
}