<!--Excel处理包-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
@Test
public void fileExcelTest() throws IOException {
Workbook workbook = new XSSFWorkbook("C:\\Users\\Desktop\\url.xlsx");
Sheet sheet = workbook.getSheetAt(0);
//最大行
int maxRow = sheet.getPhysicalNumberOfRows();
//去第一列的列数作为最大列
int maxCol = sheet.getRow(0).getPhysicalNumberOfCells();
StringBuilder str = new StringBuilder();
for (int i = 0; i < maxRow;i++){
Row row = sheet.getRow(i);
for (int j = 0; j < maxCol; j++) {
Cell cell = row.getCell(j);
str.append(cell).append("*");
System.out.print(cell.toString()+"*****");
}
System.out.println();
}
System.out.println(str);
}
@Test
public void fileTxtTest() throws IOException {
try {
//中文要用utf-8
String encoding="utf-8";
File file=new File("C:\\Users\\Desktop\\command.txt");
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}