這個工具類包含以下文件操作:
1、獲取文件大小
2、將文件大小自動轉換爲以“B”、“K”、“M”、“G”爲單位的的大小
3、複製文件
4、移動文件
5、刪除文件
6、解壓ZIP文件
package com.trigl.util;
import java.io.BufferedInputStream;
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.text.DecimalFormat;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* 文件處理工具類
* @author 白鑫
* 2016年7月26日 上午11:33:25
*/
public class FileUtils {
private static final int BUFFER = 1024;
/**
* 返回文件/文件夾的大小
* @param file
* @return
* @throws Exception
*/
public static long getDirSize(File file) throws Exception// 取得文件夾大小
{
long size = 0;
if (file.isDirectory()) {
File flist[] = file.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getDirSize(flist[i]);
} else {
size = size + flist[i].length();
}
}
} else {
size = file.length();
}
return size;
}
/**
* 將文件大小轉換爲適合的單位
* @param fileS
* @return
*/
public static String formetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / BUFFER) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / (BUFFER * BUFFER)) + "M";
} else {
fileSizeString = df.format((double) fileS / (BUFFER * BUFFER * BUFFER)) + "G";
}
return fileSizeString;
}
/**
* 複製文件
* @param source 被複制文件路徑
* @param target 複製後文件路徑
* @throws IOException
*/
public static void copyFile(String source, String target) throws IOException {
try (InputStream in = new FileInputStream(source)) {
try (OutputStream out = new FileOutputStream(target)) {
byte[] buffer = new byte[4096];
int bytesToRead;
// 每次讀取4096個字節
while ((bytesToRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
}
}
}
/**
* 移動某個目錄下面的所有文件到目標目錄
* @param fromPath 要移動的文件所在目錄
* @param toPath 目標目錄
*/
public void fileRemove(String fromPath,String toPath){
File fromFolder = new File(fromPath);
// fromFolder文件目錄下的子文件
File[] fromFiles=fromFolder.listFiles();
if(fromFiles==null){
return;
}
File toFolder = new File(toPath);
if (!toFolder.exists()){
toFolder.mkdirs();
}
for(int i=0;i<fromFiles.length;i++){
File file=fromFiles[i];
if (file.isDirectory()){
fileRemove(file.getPath(), toPath+"\\"+file.getName());
}
if(file.isFile()){
File toFile=new File(toFolder+"\\"+file.getName());
file.renameTo(toFile); // 移動操作,不要先copy再delete
}
}
}
/**
* 移動某個具體的文件到目標目錄
* @param filePath 具體文件路徑(非目錄)
* @param toPath 目標目錄
*/
public void removeFile(String filePath, String toPath){
File file = new File(filePath);
if(file.isDirectory()) {
return;
} else {
File toFile = new File(toPath + File.separatorChar +file.getName());
file.renameTo(toFile);
}
}
/**
* 刪除文件或目錄(所有的文件)
* @param path
*/
public static void delFile (String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
// 如果是文件直接刪除
if(file.isFile()) {
file.delete();
return;
}
// 如果包含子目錄遞歸刪除
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
delFile(files[i].getPath());
}
file.delete(); // 子目錄都刪除完成最後刪除該目錄
}
/**
* 解壓文件
* @param unZipFile 壓縮文件絕對路徑
* @param desPath 解壓目錄
* @return 解壓後文件
* @throws IOException
*/
public File unZipFile(String unZipFile, String desPath) throws IOException {
FileOutputStream fileOut = null;
File file = null; // 解壓後文件
System.out.print("解壓前文件路徑-->" + unZipFile);
int fileLength = (int) new File(unZipFile).length();
System.out.println(" | 文件大小-->[" + fileLength+"]字節");
ZipFile zipfile = new ZipFile(unZipFile);// 獲取該zip文件
Enumeration e = zipfile.entries();
byte[] buf = new byte[fileLength*2];
int readedBytes;
try {
// 遍歷壓縮包中所有壓縮文件
while (e.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) e.nextElement();
file = new File(desPath + File.separatorChar + zipEntry.getName());
if(file.exists()){
file.delete();
}
if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fileOut = new FileOutputStream(file.getPath());
BufferedInputStream b = new BufferedInputStream(zipfile.getInputStream(zipEntry));
while ((readedBytes = b.read(buf)) > 0) {
fileOut.write(buf, 0, readedBytes);
}
fileOut.close();
}
}
System.out.print("解壓後文件路徑-->"+file.getPath());
int fileLength1 = (int) file.length();
System.out.println(" | 文件大小-->[" + fileLength1+"]字節");
return file;
}catch (Exception ioe) {
ioe.printStackTrace();
return null;
}finally{
zipfile.close();
fileOut.close();
}
}
}
Men were born to be suffering, the pain of struggle, or the pain of regret?