卖逼视频免费看片|狼人就干网中文字慕|成人av影院导航|人妻少妇精品无码专区二区妖婧|亚洲丝袜视频玖玖|一区二区免费中文|日本高清无码一区|国产91无码小说|国产黄片子视频91sese日韩|免费高清无码成人网站入口

用JAVA實現(xiàn)根據(jù)Excel指定編號復(fù)制圖片到文件夾

在計算機(jī)編程領(lǐng)域,利用JAVA語言操作Excel表格并處理其中的數(shù)據(jù)是一項常見任務(wù)。本文將介紹如何使用JAVA和Apache POI庫,根據(jù)Excel表格中指定的編號,將對應(yīng)編號的圖片從源文件夾復(fù)制到

在計算機(jī)編程領(lǐng)域,利用JAVA語言操作Excel表格并處理其中的數(shù)據(jù)是一項常見任務(wù)。本文將介紹如何使用JAVA和Apache POI庫,根據(jù)Excel表格中指定的編號,將對應(yīng)編號的圖片從源文件夾復(fù)制到目標(biāo)文件夾。這個過程涉及到創(chuàng)建Maven項目、讀取Excel表格、復(fù)制文件等多個步驟,讓我們逐步展開。

創(chuàng)建Maven Java項目

首先,在Eclipse中新建一個Maven Java項目,并在項目的`pom.xml`文件中引入Apache POI庫的依賴。更新Maven項目以確保依賴正確加載。

```xml

org.apache.poi

poi

3.17

```

接下來,創(chuàng)建一個Copy類,并實現(xiàn)使用FileChannel復(fù)制文件的方法。

復(fù)制文件

```java

private static void copyFileUsingFileChannels(String sourcePath, String destPath) throws IOException {

FileChannel inputChannel null;

FileChannel outputChannel null;

FileInputStream fis null;

FileOutputStream fos null;

File source null;

File dest null;

try {

source new File(sourcePath);

dest new File(destPath);

fis new FileInputStream(source);

fos new FileOutputStream(dest);

inputChannel ();

outputChannel ();

(inputChannel, 0, ());

} catch (IOException e) {

();

} finally {

();

();

();

();

}

}

```

獲取文件夾下圖片名稱集合

實現(xiàn)一個方法來獲取文件夾下所有圖片的名稱集合,并去除文件后綴。

```java

public static List getImgList(String path) {

File imgFile new File(path);

List imgNameList new ArrayList<>();

String[] list ();

if (list ! null) {

for (String name : list) {

((0, name.length() - 4));

}

}

return imgNameList;

}

```

獲取Excel表格中的人員編號集合

編寫方法來獲取Excel表格中的人員編號集合,這里使用Apache POI庫來讀取Excel文件。

```java

public static List getNoList(String path) {

ArrayList list new ArrayList<>();

try {

InputStream inputStream new FileInputStream(path);

HSSFWorkbook workbook new HSSFWorkbook(inputStream);

HSSFSheet sheet1 (0);

for (Row row : sheet1) {

if (() < 1) {

continue;

}

String number (0).getStringCellValue();

(number);

}

();

} catch (IOException e) {

();

}

return list;

}

```

執(zhí)行整個過程

最后,實現(xiàn)一個方法來執(zhí)行整個過程:根據(jù)Excel中的編號,查找源文件夾中對應(yīng)編號的圖片并復(fù)制到目標(biāo)文件夾。

```java

public static void operateAll(List imgList, List noList, String sourcePath, String destPath) {

for (String no_excel : noList) {

for (String no_img : imgList) {

if (no_excel.equals(no_img)) {

try {

copyFileUsingFileChannels(sourcePath "/" no_img ".jpg", destPath "/" no_img ".jpg");

} catch (IOException e) {

();

}

}

}

}

}

```

通過以上步驟,即可實現(xiàn)根據(jù)Excel表格中的編號,將對應(yīng)圖片復(fù)制到指定文件夾的功能。執(zhí)行main方法,可以完成整個流程。在源文件夾有10張jpg格式的圖片,目標(biāo)文件夾為空時,執(zhí)行后目標(biāo)文件夾中會有5張圖片,完整展現(xiàn)了整個過程。

這樣的操作不僅提高了工作效率,也展示了JAVA編程在文件處理方面的強(qiáng)大功能。希望本文能夠幫助您更好地理解如何利用JAVA操作Excel表格并處理文件。

標(biāo)簽: