文件路径在代码中没有改变

File Path not changing in code

所以我创建了一个 class 来搜索 excel 文件并打印文件的所有行和列。现在我有一个异常文件未找到错误。为了修复此错误,我更改了代码中的文件路径,但由于某种原因,我得到了相同的错误,并且控制台中的错误消息显示了我使用的先前文件路径,这给了我错误。

我知道新的文件路径是正确的,但 Eclipse 似乎无法识别代码已更新。这是来自 class:

的代码
public class ExcelReader {
    public static final String SAMPLE_XLSX_FILE_PATH = "K:\Documents\Project\Netword_GUI\Netword_GUI\src\libs\cc2017.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {

        // Creating a Workbook from an Excel file (.xls or .xlsx)
        Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));

        // Retrieving the number of sheets in the Workbook
        System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");

        /*
           =============================================================
           Iterating over all the sheets in the workbook (Multiple ways)
           =============================================================
         */

        // You can obtain a sheetIterator and iterate over it
        Iterator<Sheet> sheetIterator = workbook.sheetIterator();
        System.out.println("Retrieving Sheets using Iterator");
        while (sheetIterator.hasNext()) {
            Sheet sheet = sheetIterator.next();
            //System.out.println(sheet.getRow(0));
            System.out.println("=> " + sheet.getSheetName());
        }
        // Getting the Sheet at index zero
        Sheet sheet = workbook.getSheetAt(0);

        // Create a DataFormatter to format and get each cell's value as String
        DataFormatter dataFormatter = new DataFormatter();

        // You can obtain a rowIterator and columnIterator and iterate over them
        System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            // Now let's iterate over the columns of the current row
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellValue = dataFormatter.formatCellValue(cell);
                System.out.print(cellValue + "\t");
            }
            System.out.println();
        }
        if (sheet.getActiveCell() == null) {
            // Closing the workbook
            workbook.close();

        }
    }
} 

控制台中显示的错误信息如下:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\User\Dropbox\Placement\Private_Backup\Netword_GUI\Netword_GUI\src\cc2017.xlsx at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:250) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:226) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:205) at ExcelReader.main(ExcelReader.java:13)

注意:控制台中的文件路径与代码中的文件路径不同。以前错误消息中的文件路径在代码中这是因为我在另一台计算机上有相同的 class 所以文件路径被更改,因为我当前正在使用的计算机上的位置不同。

能否请你添加你的代码,让每个人都能看到你的代码的大图,给你一个正确的答案 试试这个

   File f=new File(fixed_file_path);  
   Workbook workbook = WorkbookFactory.create(f);

有时 Eclipse 中的构建不是最新的。

在这种情况下,您应该清理并重新构建您的项目(从“项目”菜单)