org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)

烦请老师看一下,为什么会报错呢?~谢谢(感觉是 workbook没有获取到值~~但不知道为什么~)

报错信息如下:


代码如下:
package com.hogwarts.junit5readFile;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.endsWithIgnoringCase;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JunitExcelTest {

@Test
@DisplayName("读取excel文件内容放入一个列表中")
void withExcelFile() throws FileNotFoundException {
    try {
    //1、 获取文件名后缀
        String filepath = "src/test/resources/source.xlsx";
        String excelType = filepath.substring(filepath.lastIndexOf(".")+1,filepath.length());
        assertThat("当前文件不是excel文件",excelType,anyOf(endsWithIgnoringCase("xlsx"),endsWithIgnoringCase("xls")));
        //2、查看文件是否存在
        File file = new File(filepath);
        assertTrue(file.exists(),"文件不存在");
        //3、获取工作薄对象Work --如果是xls结尾用HSSF--如果是xlsx结尾用XSSF
        FileInputStream stream = new FileInputStream(file);
        Workbook workbook = null;

        if(excelType.equalsIgnoreCase("xlsx")){
             workbook = new XSSFWorkbook(stream);
        }else if(excelType.equalsIgnoreCase("xls")) {
             workbook = new HSSFWorkbook(stream);
        }
        //对应sheet是从0开始的
        Sheet sheet =  workbook.getSheetAt(0);
        //当前工作薄有多少个sheet
        int numsheet = workbook.getNumberOfSheets();
        System.out.println("当前工作薄有"+numsheet+"sheet");

        //当前sheet有多少行
        int numrows = sheet.getPhysicalNumberOfRows();
        System.out.println("当前sheet有"+numrows+"行");
        for (Row row:sheet) {
            System.out.println(row);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    //4、读取第一个sheet
    //5、获取行
    //6、读取每一行的单元格内容
    //7、最后关闭流

}

POM文件如下:

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<groupId>org.example</groupId>
<artifactId>Junit5Basics</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>

    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>

</properties>

<dependencies>
    <!--junit5-->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite-api</artifactId>
        <version>1.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <version>1.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.2</version>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest</artifactId>
        <version>2.2</version>
        <scope>test</scope>
    </dependency>
    <!--        读取文件公共依赖-->
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-csv</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>


</dependencies>

<build>
    <plugins>
        <!--maven运行插件-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
        </plugin>

    </plugins>
</build>

问题出在excel表上
在电脑上创建的excel表直接放入source文件夹,在source文件夹中打开时,会提示

image

在运行代码时也无法读取excel表中的信息

通过代码生成一个excel表,然后再添加后缀,打开生成的excel表,进行数据输入
再运行代码时未报错
(感觉这是个笨办法,应该有更好的解决方式,只是目前还知道~~~ :face_holding_back_tears:
生成excel表代码:

void withCreatExcel() throws IOException {
//创建路径
String path = “src/test/resources”;
//定义excel文件名称
String fliename = “datasource.xls”;
//创建一个工作薄
Workbook workbook = new HSSFWorkbook();
//创建一个工作表
Sheet sheet1 = workbook.createSheet(“读取示范”);
//创建第一行
Row row1 = sheet1.createRow(0);
//创建第一行,第一列
Cell cell11 = row1.createCell(0);
cell11.setCellValue(“字符串类型”);
//创建第一行,第二列
Cell cell12 = row1.createCell(1);
cell12.setCellValue(“布尔类型”);

    //用IO流生成一张excel表,后缀为.xls
    FileOutputStream fileOutputStream = new FileOutputStream(path+ File.separator+fliename);
    //写入
    workbook.write(fileOutputStream);
    //关流
    fileOutputStream.close();
    System.out.println(fliename+"生成完毕!!");
}