文件路径未在代码中更改

时间:2018-07-16 12:01:18

标签: java filepath eclipse-oxygen

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

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

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();

        }
    }
} 

现在这是控制台中显示的错误消息:

  

线程“ main”中的异常java.io.FileNotFoundException:   C:\ Users \ User \ Dropbox \ Placement \ Private_Backup \ Netword_GUI \ Netword_GUI \ src \ cc2017.xlsx     在   org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:250)     在   org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:226)     在   org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:205)     在ExcelReader.main(ExcelReader.java:13)

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

能否请您添加代码,以使每个代码都对您的代码有个全面的了解,从而为您提供正确的答案   试试这个

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