java將一個(gè)目錄下的所有文件復(fù)制n次
本文實(shí)例為大家分享了java將一個(gè)目錄下的所有文件復(fù)制n次的具體代碼,供大家參考,具體內(nèi)容如下
1. 文件復(fù)制示意圖
2.java程序
(1).調(diào)用
final static String SOURCESTRING = "/Users/amarao/360/download/test/"; final static String OUTPUTSTRING = "/Users/amarao/360/download/test4/"; public static void main(String[] args) throws IOException { // 將SOURCESTRING下的文件復(fù)制3次到OUTPUTSTRING目錄下 LCopyFileUtils.copyFile(SOURCESTRING, OUTPUTSTRING, 3); }
(2).java工具類
/** * * 參考: * Java將一個(gè)目錄下的所有數(shù)據(jù)復(fù)制到另一個(gè)目錄下:http://chabaoo.cn/article/167726.htm * Java復(fù)制文件的4種方式:http://chabaoo.cn/article/70412.htm * */ public class LCopyFileUtils { /** * 復(fù)制srcPath路徑下的文件到destPath目錄下 * * @param srcPath 源文件路徑 * @param destPath 輸出路徑 * @param count 每個(gè)文件的復(fù)制次數(shù) * @return 是否復(fù)制成功 */ public static boolean copyFile(String srcPath, String destPath, int count) throws IOException { File fileSrc = new File(srcPath); File[] files = fileSrc.listFiles(); if (files == null) { System.out.println("Error:源文件夾下沒有文件"); return false; } for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { File file = null; String fileName = files[i].getName(); String filePrefix = fileName.substring(0, fileName.lastIndexOf(".")); String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); // 每個(gè)文件復(fù)制Count次 for (int j = 0; j < count; j++) { file = new File(destPath + File.separator + filePrefix + "_" + i + "_" + j + fileSuffix);// 創(chuàng)建文件 copyFileUsingFileChannels(files[i], file); } } } return true; } /** * 復(fù)制文件srcFile到destFile * * @param srcFile 源文件 * @param destFile 目的文件 */ public static void copyFileUsingFileChannels(File srcFile, File destFile) throws IOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(srcFile).getChannel(); outputChannel = new FileOutputStream(destFile).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); System.out.println("復(fù)制文件成功:" + srcFile.getName() + " -> " + destFile.getName()); } catch (Exception e) { System.out.println("Error:復(fù)制文件失?。? + srcFile.getName() + " -> " + destFile.getName()); } finally { if (inputChannel != null) { inputChannel.close(); } if (outputChannel != null) { outputChannel.close(); } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java簡(jiǎn)單實(shí)現(xiàn)復(fù)制 粘貼 剪切功能代碼分享
- java實(shí)現(xiàn)文本復(fù)制功能
- Java 添加、修改、讀取、復(fù)制、刪除Excel批注的實(shí)現(xiàn)
- Java實(shí)現(xiàn)inputstream流的復(fù)制代碼實(shí)例
- 利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- Java 圖片復(fù)制功能實(shí)現(xiàn)過程解析
- Java對(duì)象的復(fù)制三種方式(小結(jié))
- java遞歸實(shí)現(xiàn)復(fù)制一個(gè)文件夾下所有文件功能
- java將一個(gè)目錄下的所有數(shù)據(jù)復(fù)制到另一個(gè)目錄下
- Java加速讀取復(fù)制超大文件
- 詳解Java中IO字節(jié)流基本操作(復(fù)制文件)并測(cè)試性能
- Java 訪問剪切板(復(fù)制,粘貼)的示例
相關(guān)文章
Springboot升級(jí)至2.4.0中出現(xiàn)的跨域問題分析及修改方案
這篇文章主要介紹了Springboot升級(jí)至2.4.0中出現(xiàn)的跨域問題分析及修改方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12springboot以FTP方式上傳文件到遠(yuǎn)程服務(wù)器
這篇文章主要介紹了springboot以FTP方式上傳文件到遠(yuǎn)程服務(wù)器,需要的朋友可以參考下2019-12-12mall整合SpringTask實(shí)現(xiàn)定時(shí)任務(wù)的方法示例
這篇文章主要介紹了mall整合SpringTask實(shí)現(xiàn)定時(shí)任務(wù)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06idea創(chuàng)建springboot項(xiàng)目(版本只能選擇17和21)的解決方法
idea2023創(chuàng)建spring boot項(xiàng)目時(shí),java版本無法選擇11,本文主要介紹了idea創(chuàng)建springboot項(xiàng)目(版本只能選擇17和21),下面就來介紹一下解決方法,感興趣的可以了解一下2024-01-01SpringBoot接口實(shí)現(xiàn)百萬并發(fā)的代碼示例
隨著互聯(lián)網(wǎng)的發(fā)展,越來越多的應(yīng)用需要支持高并發(fā),在這種情況下,如何實(shí)現(xiàn)高并發(fā)成為了一個(gè)重要的問題,Spring Boot是一個(gè)非常流行的Java框架,它提供了很多方便的功能來支持高并發(fā),本文將介紹如何使用Spring Boot來實(shí)現(xiàn)百萬并發(fā)2023-10-10Java JDK與cglib動(dòng)態(tài)代理有什么區(qū)別
這篇文章主要介紹了Java JDK動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理的區(qū)別文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03Java手機(jī)號(hào)碼工具類示例詳解(判斷運(yùn)營商、獲取歸屬地)
這篇文章主要介紹了Java手機(jī)號(hào)碼工具類示例詳解,通過手機(jī)號(hào)碼來判斷運(yùn)營商獲取歸屬地,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02