java解压文件、复制文件、删除文件代码示例
生活随笔
收集整理的這篇文章主要介紹了
java解压文件、复制文件、删除文件代码示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 刪除文件:
- 創建目錄
- 拷貝文件
- 解壓zip文件
解壓文件時,可以采用多線程的方式,下面上代碼:
創建類
刪除文件:
public static boolean deleteFile(File file) {if (file == null) {return false;}if (!file.exists()) {return false;}if (file.isFile()) {return file.delete();} else {for (File newfile : file.listFiles()) {deleteFile(newfile);}}return file.delete();}/*** 刪除文件** @param fileUrl 文件路徑* @return 刪除是否成功*/public static boolean deleteFile(String fileUrl) {File file = newFile(fileUrl);return deleteFile(file);}//參數為路徑時為需要刪除的文件路徑public static boolean deleteFile(String fileUrl) {File file = newFile(fileUrl);return deleteFile(file);}創建目錄
public static boolean mkdir(File file) {if (file == null) {return false;}if (file.exists()) {return true;}return file.mkdirs();}/*** 創建目錄** @param fileUrl 文件路徑* @return 是否創建成功*/public static boolean mkdir(String fileUrl) {if (fileUrl == null) {return false;}File file = newFile(fileUrl);if (file.exists()) {return true;}return file.mkdirs();}拷貝文件
/*** 拷貝文件** @param fileInputStream 文件輸入流* @param fileOutputStream 文件輸出流* @throws IOException io異常*/public static void copyFile(FileInputStream fileInputStream, FileOutputStream fileOutputStream) throws IOException {try {byte[] buf = new byte[4096]; //8k的緩沖區int len = fileInputStream.read(buf); //數據在buf中,len代表向buf中放了多少個字節的數據,-1代表讀不到while (len != -1) {fileOutputStream.write(buf, 0, len); //讀多少個字節,寫多少個字節len = fileInputStream.read(buf);}} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}/*** 拷貝文件** @param src 源文件* @param dest 目的文件* @throws IOException io異常*/public static void copyFile(File src, File dest) throws IOException {FileInputStream in = new FileInputStream(src);FileOutputStream out = new FileOutputStream(dest);copyFile(in, out);}/*** 拷貝文件** @param srcUrl 源路徑* @param destUrl 目的路徑* @throws IOException io異常*/public static void copyFile(String srcUrl, String destUrl) throws IOException {if (srcUrl == null || destUrl == null) {return;}File srcFile = newFile(srcUrl);File descFile = newFile(destUrl);copyFile(srcFile, descFile);}解壓zip文件
/*** 文件解壓縮** @param sourceFile 需要解壓的文件* @param destDirPath 目的路徑* @return 解壓目錄列表*/public static List<String> unzip(File sourceFile, String destDirPath) {ZipFile zipFile = null;Set<String> set = new HashSet<String>();// set.add("/");List<String> fileEntryNameList = new ArrayList<>();try {zipFile = new ZipFile(sourceFile, Charset.forName("GBK"));Enumeration<? extends ZipEntry> entries = zipFile.entries();while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();String[] nameStrArr = entry.getName().split("/");String nameStr = "/";for (int i = 0; i < nameStrArr.length; i++) {if (!"".equals(nameStrArr[i])) {nameStr = nameStr + "/" + nameStrArr[i];set.add(nameStr);}}log.info("解壓" + entry.getName());String zipPath = "/" + entry.getName();fileEntryNameList.add(zipPath);//如果是文件夾,就創建個文件夾if (entry.isDirectory()) {String dirPath = destDirPath + File.separator + entry.getName();File dir = FileOperation.newFile(dirPath);dir.mkdir();} else {//如果是文件,就先創建一個文件,然后用io流把內容拷過去File targetFile = new File(destDirPath + "/" + entry.getName());// 保證這個文件的父文件夾必須要存在if (!targetFile.getParentFile().exists()) {targetFile.getParentFile().mkdirs();}targetFile.createNewFile();// 將壓縮文件內容寫入到這個文件中InputStream is = null;FileOutputStream fos = null;try {is = zipFile.getInputStream(entry);fos = new FileOutputStream(targetFile);int len;byte[] buf = new byte[2048];while ((len = is.read(buf)) != -1) {fos.write(buf, 0, len);}} catch (Exception e) {// 關流順序,先打開的后關閉if (fos != null) {try {fos.close();} catch (Exception e1) {log.error("關閉流失敗:" + e1);}}if (is != null) {try {is.close();} catch (Exception e2) {log.error("關閉流失敗:" + e2);}}}}}} catch (Exception e) {throw new RuntimeException("unzip error from ZipUtils", e);} finally {if (zipFile != null) {try {zipFile.close();} catch (IOException e) {e.printStackTrace();}}}for (String zipPath : fileEntryNameList) {executor.execute(new Runnable() {@Overridepublic void run() {if (FileUtil.isImageFile(FileUtil.getFileExtendName(zipPath))) {File file = new File(destDirPath + zipPath);File minFile = new File(destDirPath + FileUtil.getFileNameNotExtend(zipPath) + "_min." + FileUtil.getFileExtendName(zipPath));try {ImageOperation.thumbnailsImage(file, minFile, 300);} catch (IOException e) {e.printStackTrace();}}}});}List<String> res = new ArrayList<>(set);return res;}其中ImageOperation.thumbnailsImage(file, minFile, 300);方法如下:
import net.coobird.thumbnailator.Thumbnails;import java.io.File; import java.io.IOException; public class ImageOperation {public static void thumbnailsImage(File inFile, File outFile, int imageSize) throws IOException {Thumbnails.of(inFile).size(imageSize, imageSize).toFile(outFile);}}上面判斷是否是圖片文件的FileUtil.isImageFile方法:
public class FileUtil {public static final String[] IMG_FILE = {"bmp", "jpg", "png", "tif", "gif", "jpeg"};public static final String[] DOC_FILE = {"doc", "docx", "ppt", "pptx", "xls", "xlsx", "txt", "hlp", "wps", "rtf", "html", "pdf"};public static final String[] VIDEO_FILE = {"avi", "mp4", "mpg", "mov", "swf"};public static final String[] MUSIC_FILE = {"wav", "aif", "au", "mp3", "ram", "wma", "mmf", "amr", "aac", "flac"};public static final int IMAGE_TYPE = 1;public static final int DOC_TYPE = 2;public static final int VIDEO_TYPE = 3;public static final int MUSIC_TYPE = 4;public static final int OTHER_TYPE = 5;public static final int SHARE_FILE = 6;public static final int RECYCLE_FILE = 7;/*** 獲取文件擴展名* @param fileName 文件名* @return 文件擴展名*/public static String getFileExtendName(String fileName) {if (fileName.lastIndexOf(".") == -1) {return "";}return fileName.substring(fileName.lastIndexOf(".") + 1);}/*** 判斷是否為圖片文件** @param extendName 文件擴展名* @return 是否為圖片文件*/public static boolean isImageFile(String extendName) {for (int i = 0; i < IMG_FILE.length; i++) {if (extendName.equalsIgnoreCase(IMG_FILE[i])) {return true;}}return false;}}總結
以上是生活随笔為你收集整理的java解压文件、复制文件、删除文件代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git回退到之前版本和合并分支查看当前分
- 下一篇: 【学习笔记】rabbitmq设置队列tt