使用text和excel文件的Java内存堆错误

时间:2016-04-20 13:16:17

标签: java

我正在编写一个执行以下任务的程序。

  1. 清理Excel文件(即根据条件删除行)。
  2. 将记事本中的数据添加到新的Excel(Say Result Excel)。
  3. 将清理过的Excel文件中的数据添加到结果Excel中。
  4. 以下是我的代码

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.apache.commons.io.FilenameUtils;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Font;
    import org.apache.poi.ss.usermodel.IndexedColors;
    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.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class TextToExcelMultiple {
        static int counter = 1;
    
        public static void main(String[] args) throws Exception {
    
            File mainFolder = new File("C:\\D\\Mypath\\New");
            File[] files;
            int rowCount = 0;
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Result");
            CellStyle style = workbook.createCellStyle();// Create style
            Font font = workbook.createFont();// Create font
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);// Make font bold
            style.setAlignment(CellStyle.ALIGN_CENTER);
            style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
            style.setFont(font);// set it to bold
            Row row;
            Cell cell;
            row = sheet.createRow(rowCount);
            cell = row.createCell(0);
            cell.setCellValue("NAME");
            cell = row.createCell(1);
            cell.setCellValue("TITLE");
            cell = row.createCell(2);
            cell.setCellValue("FIRM");
            cell = row.createCell(3);
            cell.setCellValue("ADDRESS 1");
            cell = row.createCell(4);
            cell.setCellValue("ADDRESS 2");
            cell = row.createCell(5);
            cell.setCellValue("ADDRESS 3");
            cell = row.createCell(6);
            cell.setCellValue("CITY");
            cell = row.createCell(7);
            cell.setCellValue("STATE");
            cell = row.createCell(8);
            cell.setCellValue("POSTAL CODE");
            cell = row.createCell(9);
            cell.setCellValue("COUNTRY");
            cell = row.createCell(10);
            cell.setCellValue("PHONE");
            cell = row.createCell(11);
            cell.setCellValue("FAX");
            cell = row.createCell(12);
            cell.setCellValue("URL");
            cell = row.createCell(13);
            cell.setCellValue("ISN");
            for (int i = 0; i < row.getLastCellNum(); i++) {
                row.getCell(i).setCellStyle(style);
            }
            rowCount = 1;
            if (mainFolder.isDirectory()) {
                files = mainFolder.listFiles();
                for (File file : files) {
                    if (file.isDirectory()) {
                        files = file.listFiles();
                        for (File finalFile : files) {
                            String ext = FilenameUtils.getExtension(finalFile.getAbsolutePath());
                            if ((ext.contains("xls") && finalFile.getName().toLowerCase().contains("detail"))) {
                                cleanUpFile(finalFile);
                            }
                        }
                        for (File finalFile : files) {
                            String ext = FilenameUtils.getExtension(finalFile.getAbsolutePath());
                            if ((ext.equals("txt") && finalFile.getName().contains("brief"))
                                    || (ext.contains("xls") && finalFile.getName().toLowerCase().contains("detail"))) {
                                int rowNumber = writeData(finalFile, sheet, rowCount, workbook);
                                rowCount = rowNumber;
                            }
                        }
                    }
    
                }
            }
            FileOutputStream outputStream = new FileOutputStream("C:\\D\\Mihir\\new.xls");
            workbook.write(outputStream);
            outputStream.close();
        }
    
        private static int writeData(File file, XSSFSheet sheet, int rowCount, XSSFWorkbook workbook) throws Exception {
    
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream));
            String x;
            Row row;
            Cell cell;
            String ext = FilenameUtils.getExtension(file.getAbsolutePath() + " \t " + file.getName());
            if (!ext.contains("xls")) {
                while ((x = br.readLine()) != null) {
                    if (!(x.contains("NAME"))) {
                        row = sheet.createRow(rowCount);
                        String[] namesList = x.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);
                        int columnCount = 0;
                        for (String name : namesList) {
                            cell = row.createCell(columnCount);
                            cell.setCellValue(name.replace("\"", ""));
                            columnCount += 1;
                        }
                        rowCount += 1;
                    }
                }
            }
    
            if (ext.contains("xls")) {
                int result = appendDataToExcel(file, workbook, rowCount, counter);
                counter = result;
            }
    
            br.close();
            fileInputStream.close();
            return rowCount;
    
        }
    
        private static void cleanUpFile(File file) throws Exception {
            System.out.println(file.getAbsolutePath());
            FileInputStream fin = new FileInputStream(new File(file.getAbsolutePath()));
            Workbook wb = new XSSFWorkbook(fin);
            Sheet sheet = wb.getSheetAt(0);
            Cell cell;
            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                cell = sheet.getRow(i).getCell(1);
                if (cell == null) {
                    sheet.removeRow(sheet.getRow(i));
                    int rowIndex = i;
                    int lastRowNum = sheet.getLastRowNum();
    
                    if (rowIndex >= 0 && rowIndex <= lastRowNum) {
                        sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
                    }
                }
            }
            fin.close();
            FileOutputStream outFile = new FileOutputStream(new File(file.getAbsolutePath()));
            wb.write(outFile);
            wb.close();
            outFile.close();
        }
    
        private static int appendDataToExcel(File file, XSSFWorkbook workbook, int rowCount, int counter) throws Exception {
    
            String path = file.getAbsolutePath();
            FileInputStream fin = new FileInputStream(new File(path));
            XSSFWorkbook wb = new XSSFWorkbook(fin);
            XSSFSheet sheet1 = wb.getSheetAt(0);
            int noOfRows = sheet1.getPhysicalNumberOfRows();
            XSSFSheet sheet = workbook.getSheetAt(0);
            Cell cell;
            XSSFRow row;
            for (int i = 1; i < noOfRows; i++) {
                row = sheet.getRow(counter);
                cell = row.getCell(13);
                if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                    cell = row.createCell(13);
                }
                Cell cell1 = sheet1.getRow(i).getCell(1);
                if (cell1 != null && !cell1.equals("")) {
                    cell.setCellValue(sheet1.getRow(i).getCell(1).toString() + "\t" + counter);
                }
                counter++;
            }
            wb.close();
            fin.close();
            counter = rowCount;
            return (counter);
        }
    
    }
    

    以前我曾经打电话给cleanUpFile(file)方法,而这用来给我下面的例外

    后来我尝试先清理文件,然后在清理过的文件上运行程序但是我仍然得到相同的异常。

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Unknown Source)
        at java.io.ByteArrayOutputStream.grow(Unknown Source)
        at java.io.ByteArrayOutputStream.ensureCapacity(Unknown Source)
        at java.io.ByteArrayOutputStream.write(Unknown Source)
        at org.apache.poi.openxml4j.opc.internal.MemoryPackagePartOutputStream.write(MemoryPackagePartOutputStream.java:88)
        at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:590)
        at org.apache.xmlbeans.impl.store.Cursor.save(Cursor.java:2544)
        at org.apache.xmlbeans.impl.values.XmlObjectBase.save(XmlObjectBase.java:223)
        at org.apache.poi.xssf.usermodel.XSSFSheet.write(XSSFSheet.java:2972)
        at org.apache.poi.xssf.usermodel.XSSFSheet.commit(XSSFSheet.java:2927)
        at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:323)
        at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:327)
        at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:195)
        at TextToExcelMultiple.main(TextToExcelMultiple.java:100)
    

    当我点击at TextToExcelMultiple.main(TextToExcelMultiple.java:100)时,它会指向workbook.write(outputStream);

    我的文件很大。

    还有一件事当我注释掉下面的块并运行它时,它运行正常,

    for (File finalFile : files) {
            String ext = FilenameUtils.getExtension(finalFile.getAbsolutePath());
                if ((ext.equals("txt") && finalFile.getName().contains("brief"))
                || (ext.contains("xls") && finalFile.getName().toLowerCase().contains("detail"))) {
                    int rowNumber = writeData(finalFile, sheet, rowCount, workbook);
                        rowCount = rowNumber;
                    }
         }
    

    通过其他一些SO帖子,我在我的论点中添加了以下内容。

    -Xms1100m -Xmx1100m
    

    当我尝试增加堆内存-Xms1150m -Xmx2048m时,它会给我以下错误。

    Error occurred during initialization of VM
    Could not reserve enough space for 2097152KB object heap
    

    因为我的可用内存大约是1150MB。

    enter image description here

    此外,如果我在客户的系统中将其部署为swing应用程序,那么在执行此操作之前应该注意什么?

    请告诉我如何在不面对异常的情况下运行此完整程序。

    由于

0 个答案:

没有答案