创建流而不是临时文件并使用它们并丢弃它们

时间:2012-06-27 13:48:45

标签: java servlets stream

我想写一个流并立即使用它,而不是将其写入文件和阅读。 FIles不会是线程安全的,我认为它可能会影响性能。

    //set property to an existing file
    prop.setProperty("$quoteValidity",""no");
    prop.setProperty("$date", "today");
    prop.setProperty("$quantitySum",  Integer.toString(quantitySum));

    //store it in temporary location
    prop.store(new FileOutputStream("<Temp file location>"), null);

    //open that temp file as stream
    InputStream propfile = new FileInputStream("<Temp file location>");

    List<Order> orderList = XmlToList.makeOrderFromNodeList(orderNode);
    //use that temp file
    Testground.generateXlsx(propfile,orderList);

1 个答案:

答案 0 :(得分:1)

尝试使用ByteArrayOutputStream而不是FileOuputStream。然后你可以打电话

 byte[] bytes=byteArrayOutputStream.toByteArray() 

在ByteArrayOutputStream上获取字节

然后构造一个ByteArrayInputStream(bytes)并将其传递给Testground