【问题标题】:Creating streams instead of temporary files and use them and discard them创建流而不是临时文件并使用它们并丢弃它们
【发布时间】:2012-06-27 13:48:45
【问题描述】:

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

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

【问题讨论】:

    标签: java servlets stream


    【解决方案1】:

    尝试使用 ByteArrayOutputStream 而不是 FileOuputStream。然后你可以调用

     byte[] bytes=byteArrayOutputStream.toByteArray() 
    

    在 ByteArrayOutputStream 上获取字节

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

    【讨论】:

    • 您能否详细说明我一直在搜索您的答案但无法关联,请举一个与此特定场景相关的示例,我将在我的项目中复制它
    • docs.oracle.com/javase/6/docs/api/java/io/… 记录课程。如果你用谷歌搜索“ByteArrayOutputStream howto”,intertoobz上有几十个例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 2014-01-04
    • 2015-04-19
    • 2015-09-26
    相关资源
    最近更新 更多