package com.zte;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.ArrayList;
class JarFile {
private String name;
private Long size;
public JarFile(String name, Long size) {
this.name = name;
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
}
public class Main {
public static ArrayList<JarFile> getFileSize(String dirPath) {
ArrayList<JarFile> list = new ArrayList<>();
File fileParent = new File(dirPath);
File[] files = fileParent.listFiles();
for (int i=0; i<files.length; i++) {
File file = files[i];
String fileName = file.getName();
long fileLength = file.length()%1024==0 ? file.length()/1024 : file.length()/1024+1;
System.out.println(fileLength);
JarFile jarFile = new JarFile(fileName, fileLength);
list.add(jarFile);
}
return list;
}
public static void createXlsxFile(String filePath, ArrayList<JarFile> list) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("jar");
//写数据头
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("jar包名");
row.createCell(1).setCellValue("jar包大小");
int index = 1;
for (int i=0; i<list.size(); i++) {
Row row1 = sheet.createRow(index);
int j=0;
row1.createCell(j).setCellValue(list.get(i).getName());
j++;
row1.createCell(j).setCellValue(list.get(i).getSize().toString()+" KB");
index++;
}
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (file.exists()) {
file.delete();
}
FileOutputStream out = new FileOutputStream(file);
out.flush();
wb.write(out);
out.close();
}
public static void main(String[] args) throws IOException {
//把目录下的jar包信息放入一个ArrayList<JarFile>中
ArrayList<JarFile> list = getFileSize("src/main/resources/jar");
//把list写入到xlsx文件中
createXlsxFile("src/main/resources/xlsx/jarFile.xlsx",list);
}
}