【问题标题】:Write to an excel file from multiple threads in Java从Java中的多个线程写入excel文件
【发布时间】:2015-08-12 05:04:11
【问题描述】:

我有一个 selenium 测试,可以读取和写入一个 excel 文件。读取是通过将所有数据加载到哈希图中的 dataProvider 来实现的。我现在使用 Grid 同时运行多个线程。如何使用多线程实现写作?我阅读了有关同步方法的信息,但是当我将其应用于 outputStream 文件时,我遇到了失败。

我的代码:

public static void createOutputFile(String inputFilePath) {

    inputFilePath = "InputPath";
    outputFilePath = "OutputPath";

    try {
        inputFile = new FileInputStream(new File(inputFilePath));
        workbook = WorkbookFactory.create(inputFile);
        inputFile.close();
        // save input data as output
        outputFile = new FileOutputStream(outputFilePath);
        workbook.write(outputFile);
        outputFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static synchronized void writeToFile(Integer rowNumber, Integer cellNumber, String text) {
    try {
        //recreate workbook
        FileInputStream file = new FileInputStream(new File(outputFilePath));
        workbook = WorkbookFactory.create(file);
        cell = workbook.getSheetAt(0).getRow(rowNumber).getCell(cellNumber);
        if(cell == null) 
            cell = workbook.getSheetAt(0).getRow(rowNumber).createCell(cellNumber, Cell.CELL_TYPE_BLANK);
        file.close();  
        outputFile = new FileOutputStream(outputFilePath);
        if (rowNumber != null && cellNumber != null) {
            try {
                cell.setCellValue(text);
            } catch (Exception e) {
                System.err.println("Updating the file failed " + outputFilePath); 
            }   
        }
        workbook.write(outputFile);
        outputFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 失败是什么意思?
  • 您已经阅读过同步方法,但为什么没有尝试过呢?
  • 同步你的方法 writeToFile 看看是否还有报错
  • @Adam 只是想知道:你认为学习多线程的最好方法是了解这么多复杂的上下文吗?为什么不首先关注“我如何使用多线程在 Java 中编程”?
  • @npinti:我收到此错误:org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException:保存失败:保存包时出错:/docProps/app.xml 部分无法保存使用 org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:500) 上的编组器 org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller@c0fc09e0 保存在流中。 poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1417) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:179) at

标签: java multithreading excel selenium selenium-grid


【解决方案1】:

您的代码没有显示您正在同步方法,您可以粘贴您的同步代码吗?

以防万一记住 - 同步静态方法在同步静态方法所属的类的类对象上同步,这与实例同步方法不同。

【讨论】:

    【解决方案2】:

    问题很可能出在此处:outputFile = new FileOutputStream(outputFilePath);。它的作用是创建一个指向文件开头的新流。因此,顺序调用将覆盖以前的材料。要解决这个问题,只需使用另一个版本的构造函数:outputFile = new FileOutputStream(outputFilePath, true);,它将在末尾附加流中的字节。不要忘记使您的方法同步:

    public static synchronized void writeToFile(Integer rowNumber, Integer cellNumber, String text) {
        try {
            //recreate workbook
            FileInputStream file = new FileInputStream(new File(outputFilePath));
            workbook = WorkbookFactory.create(file);
            cell = workbook.getSheetAt(0).getRow(rowNumber).getCell(cellNumber);
            if(cell == null) 
                cell = workbook.getSheetAt(0).getRow(rowNumber).createCell(cellNumber, Cell.CELL_TYPE_BLANK);
            file.close();  
            outputFile = new FileOutputStream(outputFilePath, true);
            if (rowNumber != null && cellNumber != null) {
                try {
                    cell.setCellValue(text);
                } catch (Exception e) {
                    System.err.println("Updating the file failed " + outputFilePath); 
                }   
            }
            workbook.write(outputFile);
            outputFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 伙计,你不是说 new FileInputStream(new File(outputFilePath),true);代替?
    • @nafas:根据我的理解,outputFile 流被用于写入。此外,FileInputStream 似乎没有类似于您指定的构造函数。
    • 你是对的伙伴,仍然认为代码行不通,我认为它需要完全重新设计(不是你的代码而是整个程序)才能使其用于多线程应用程序
    • @nafas:我明白你的意思。该方法看起来非常独立,因此添加 synchronized 关键字应该会有所帮助,但就提供的信息而言,我想说我的回答纯粹是推测性的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多