细节优化

This commit is contained in:
彭宇琦 2020-12-26 16:47:29 +08:00
parent 76804204f1
commit 6c7c7321dc
5 changed files with 3 additions and 156 deletions

View File

@ -1,37 +0,0 @@
package pres.auxiliary.tool.file;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
* <p><b>文件名</b>DisposeFile.java</p>
* <p><b>用途</b>用于满足日常中对文本文件的处理</p>
* <p><b>编码时间</b>2019年7月12日上午10:40:25</p>
* <p><b>修改时间</b>2019年7月12日上午10:40:25</p>
* @author 彭宇琦
* @version Ver1.0
* @since JDK 12
*
*/
public class DisposeFile {
/**
* 用于读取文件并将文件的信息有序地返回
* @param tagerFile 目标文件
* @return 文件中的内容
* @throws IOException
*/
public static ArrayList<String> readFile(File tagerFile, int sheetIndex) throws IOException {
// 用于存储文件的后缀名以判断文件的格式
String[] fileName = tagerFile.getName().split("\\.");
String suffix = fileName[fileName.length - 1];
switch (suffix) {
case "xls":
case "xlsx":
return ReadFile.readExcel(tagerFile, sheetIndex);
default:
throw new UnsupportedFileException("无法解析“" + suffix + "”文件格式");
}
}
}

View File

@ -9,7 +9,7 @@ import org.apache.poi.ss.usermodel.IndexedColors;
* <p><b>修改时间</b>2020年2月25日上午8:31:14</p>
* @author 彭宇琦
* @version Ver1.0
* @since JDK 12
* @since JDK 1.8
*
*/
public enum MarkColorsType {

View File

@ -1,78 +0,0 @@
package pres.auxiliary.tool.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadFile {
/**
* 用于对Excel文件的后缀判定
*/
private static String EXCEL = "xlsx";
public static ArrayList<String> readExcel(File tagerFile, int sheetIndex) throws IOException {
ArrayList<String> text = new ArrayList<String>();
// 读取文件流
FileInputStream fip = new FileInputStream(tagerFile);
// 用于读取excel
Workbook excel = null;
// 根据文件名的后缀对其判断文件的格式并按照相应的格式构造对象
if (tagerFile.getName().indexOf(EXCEL) > -1) {
// 通过XSSFWorkbook对表格文件进行操作
excel = new XSSFWorkbook(fip);
} else {
// 通过XSSFWorkbook对表格文件进行操作
excel = new HSSFWorkbook(fip);
}
// 关闭流
fip.close();
// 读取下标对应的sheet
Sheet sheet = excel.getSheetAt(sheetIndex);
// 循环读取所有的行
for (int i = 0; i < sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
// 循环根据列读取单元格
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
text.add(cellValueToString(cell));
}
}
excel.close();
return text;
}
/**
* 将单元格的内容转换为字符串
* @param cell 单元格对象
* @return 转换后的字符串
*/
private static String cellValueToString(Cell cell) {
//如果单元格未被创建则返回空串
if (cell == null) {
return "";
}
//判断单元的的样式为数字则区分是否为日期否则直接返回其值
if (cell.getCellTypeEnum() == CellType.NUMERIC) {
//判断单元格的样式是否为日期格式若是日期格式则按照yyyy年MM月dd日 HH:mm:ss的格式进行转换若为数字则去除其整数的.0
if (!cell.getCellStyle().getDataFormatString().equalsIgnoreCase("General")) {
return new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(cell.getDateCellValue());
} else {
return cell.toString().replaceFirst("\\.0", "");
}
} else {
return cell.toString();
}
}
}

View File

@ -70,7 +70,7 @@ public class RecognitionImage {
try {
imageio = ImageIO.read(imageFile);
} catch (IOException e) {
e.printStackTrace();
throw new IncorrectDirectoryException("文件有误,无法读取");
}
if ( judge_Language.equals(language) ) {
@ -99,7 +99,7 @@ public class RecognitionImage {
// 读取图片并设置需要读取的位置
result = instance.doOCR(imageFile, new Rectangle(x, y, width, height));
} catch (TesseractException e) {
e.printStackTrace();
throw new IncorrectDirectoryException("文件有误,无法读取");
}
return result;

View File

@ -1,38 +0,0 @@
package pres.auxiliary.work.selenium.tool;
/**
* <p><b>文件名</b>RecordStateException.java</p>
* <p><b>用途</b>当记录状态不正确的情况下抛出的异常</p>
* <p><b>编码时间</b>2019年10月6日下午5:55:27</p>
* <p><b>修改时间</b>2019年10月6日下午5:55:27</p>
* @author 彭宇琦
* @version Ver1.0
* @since JDK 12
*
*/
public class RecordStateException extends RuntimeException {
private static final long serialVersionUID = 1L;
public RecordStateException() {
super();
}
public RecordStateException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public RecordStateException(String message, Throwable cause) {
super(message, cause);
}
public RecordStateException(String message) {
super(message);
}
public RecordStateException(Throwable cause) {
super(cause);
}
}