引言java
在JavaWeb項目開發過程,涉及到IO文件的讀寫操做以及文件的複製copy操做是做爲一個程序員不可獲取的知識,那接下來就總結一些copy文件的一些方法,與你們經過學習,若是還有其餘更好的方法,歡迎你們留言探討.代碼以下:nginx
package com.svse.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;程序員
/***
*
*功能說明:複製文件 將FileA 複製爲FileB文件
*@author:zsq
*create date:2019年5月30日 下午2:38:20
*修改人 修改時間 修改描述
*
*Copyright (c)2019北京智華天成科技有限公司-版權全部
*/
public class FileUtils {apache
//(方法一)copy複製文件 將FileA 複製爲FileB文件
@SuppressWarnings("unused")
private static void copyFileUsingFileStreams1(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}學習
}
//(方法二)copy複製文件 將FileA 複製爲FileB文件
@SuppressWarnings("unused")
private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
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();
}ui
}
//(方法三)copy複製文件 將FileA 複製爲FileB文件
@SuppressWarnings("unused")
private static void copyFileUsingApacheCommonsIO(File source, File dest)
throws IOException {
org.apache.commons.io.FileUtils.copyFile(source, dest);
}
spa
//(方法四)copy複製文件 將FileA 複製爲FileB文件
@SuppressWarnings("unused")
private static void copyFileUsingJava7Files(File source, File dest)
throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
code
/**
*
*功能說明:將zip文件解壓到指定的目錄
*輸入參數:zipFile待解壓的文件 descDir解壓到的目錄
*輸出參數:
*建立人:zsq
*建立時間:2019年5月30日 下午3:04:16
*
*/
public static void unZipFiles(File zipFile, String descDir) throws IOException{
ZipFile zip = new ZipFile(zipFile,Charset.forName("GBK"));//解決中文文件夾亂碼
String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));
File pathFile = new File(descDir+name);
if (!pathFile.exists()) {
pathFile.mkdirs(); //以給定的路徑加上待加壓文件的文件名建立文件夾
}
for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
//String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");
String outPath = (descDir +"/"+ zipEntryName).replaceAll("\\*", "/");
// 判斷路徑是否存在,不存在則建立文件路徑
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
}
// 判斷文件全路徑是否爲文件夾,若是是上面已經上傳,不須要解壓
if (new File(outPath).isDirectory()) {
continue;
}
// 輸出文件路徑信息
System.out.println(outPath);
FileOutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
System.out.println("******************解壓完畢********************");
return;
}blog
public static void main(String[] args) throws IOException {
//copyFileUsingFileStreams1(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc11.txt"));
//copyFileUsingFileChannels(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc22.txt"));
//copyFileUsingApacheCommonsIO(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc33.txt"));
copyFileUsingJava7Files(new File("E:/Ng/test/abc.txt"),new File("E:/Ng/test/abc44.txt"));
//unZipFiles(new File("E:/Ng/test/nginx-1.12.2.zip"),"E:/Ng/test");
}ip
}
結果以下: