根据Excel文件中的内容,修改指定文件夹下的文件名称

问题:根据Excel文件中内容,把文件名称由第2列,改为第1列。比如:把文件“123.jpg”修改为“1.jpg”。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQsAAABWCAIAAABXUCSYAAAGXklEQVR4nO2bXXLiRhCAlSNl7+KnXCbvOcceRfwtMV4SljIOMWUcijIuihuQB2spWZqfntGoe7rVX80DRsKabvU3I2mgWC5/aDO2/f4/bd5Gfpr6bgV5irVpy7mpIdq0uVpxuVwul8v7+/tFIufzmboL0jifz+vDQl778XpvjLcy5O3tDTHJeJxOJ+ouSON0OpFXcx9t9eI05Hg8IiYZD6lxEXI8HsmruY/29+5PY7yVIYfDATHJeEiNi5DD4UBezX205b9zY7yVIa+vr4hJxmO/31N3QRr7/Z68mvto3/9xGvLy8oKW4ru7O7Rj7XY7tGNFcPcZ6u6A2O12OCX76x+/1Fvfh3vYfDPGWxny/PyMlmLMUthut2jHiqOeDRaSbLdbHD0cf/bRFuuZMd7iFjZaijHHy81mg3OgaNgZstlsRBpyv3Ia8vT0hJPfjyJAK4X1eo1zoGjYGbJer5H1wGnzv6bGeCtDHh8fcfKLbMhqtcI5UDTs7kNWq5VIQ759nxjjrQxBG2tvdYBTEMvlEuEoXWA3hyyXS5GGzBZOQ9DGWuQhc7FYIBylC4085C/JYtF7sZIYMp2PjfFWhuCMtfjj5XxufsidD+wMmc/nIg2ZzEbGeCtDHh4eEJKLXw3Tqfn2Kx/YGTKdThHqFf9Z1njiNOT+3vy1rbTgV8N4bJ4684Hdfch4PEY2BGdKKUelMd7KEISrkcbtB87dSFmaw84EjmvqZVki1Ou6tqaOczhbqVSGzGbm5RLuZG4IR9AMQW4eQyYT86Mu7qghyfnt6xepzRhvMZlMRqNRqSgwyOu4v2aMt5pDyrK8SqT4vag36u5IQGqp2OIakCHUfRGC1FIZuiHUHZGDu1SKgmuqB20IdS9EcSuV4jO3N+s7MxJmuIZQd0EaxlIBGlLA6K/zDoINYWS/A2BcttN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Q+FlAI9IQ7qc51dPeLvcnwlBDrlf74ho7HF8isr1v3NT+z6H7i0ENEYXUuAi5pbQ9sjiI2J8qrgZqiBKG1JRaDen4G0VFkY3OIUoYUlNqi0sNUcKQmlI1REmD1JSqIUoapKZUDVHSIDWlaoiSBqkpVUOUNEhNqS2u/wHpv15JBkfqUQAAAABJRU5ErkJggg==" alt="" />

思路1:按行读取Excel文件,同时遍历文件;修改文件名称。(双层for循环,效率:n*n)

* 思路2:把文件以Map的形式进行一一存储;读取Excel文件,从Map中取得文件;修改文件名称。(效率:n+n)


/**
* 描述:根据Excel中的内容,修改目标路径下的文件名称
* @param excelFile 源Excel文件(最原始文件,3列的)
* @param path 文件夹的路径
* @param column 表格中的第几列。0序号;2姓名
* @throws CheckException
*/
public static void updateFileName(File excelFile,File path,Integer column) throws CheckException{
checkExcelFile(excelFile);
if(path.listFiles()==null){
throw new CheckException("照片文件不存在!");
}
//1.遍历文件,把文件存储至 Map中。键:文件名称(无后缀),值:文件
File[] files=path.listFiles();
Map<String,File> fileMap=new HashMap<String,File>();
for(File file:files ){
String name=file.getName();
String prefixPhoto=name.substring(0,name.lastIndexOf('.'));
fileMap.put(prefixPhoto,file);
} try{
    //2.遍历Excel文件
Workbook workbook=new HSSFWorkbook(new FileInputStream(excelFile));
Sheet sheet = workbook.getSheetAt(0); // 从工作区中取得页(Sheet)
int rowEnd = sheet.getLastRowNum();
for (int i = 0; i < rowEnd; i++) {//行 循环打印Excel表中的内容
Row row = sheet.getRow(i);
String idcard = row.getCell(1).getStringCellValue();//(列,行)
File photoFile=fileMap.get(idcard);
       //3.修改文件名称
if(photoFile!=null){
String fileName=photoFile.getName();
String subfixPhoto=fileName.substring(fileName.lastIndexOf('.'));
row.getCell(column).setCellType(Cell.CELL_TYPE_STRING);//设置Excel文件中单元格内容的类型为String类型
String text = row.getCell(column).getStringCellValue();
photoFile.renameTo(new File(photoFile.getParent()+"\\"+text+subfixPhoto));//修改文件名称
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
上一篇:Tesseract API在VS 2013中的配置以及调用


下一篇:Android编程 获取网络连接状态 及调用网络配置界面