Getting Stream closed when reading/writing to file Java
<p>I'm implementing a small tool in Java. I have a excel document and from every sheet I need to generate a .sql file. I've created an sql file model, which I have to read from for every excel sheet then replace a value and write it back to another .sql file. The problem is I use a for where I loop through my sheets and for every sheet I need to read that sql file, modify it and export it somewhere else. I get a "Stream closed" error, and I don't know how to close my buffer and/or my InputStream properly. Can you guys help me out with this ?</p>

<p>This is my code:
This gets everything from the file and converts it to a String</p>

<pre><code>public String getString(InputStream is) throws IOException {

    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    String line;

    try {
        reader = new BufferedReader(new InputStreamReader(is));

        while ((line = reader.readLine()) != null) {
            sb.append(line + System.lineSeparator());
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
return sb.toString();
}
</code></pre>

<p>This is used to export the file</p>

<pre><code>public void exportFile(String text, String path, String name, String extension) {

    BufferedWriter output = null;

    try {
        File sqlFile = new File(path + name + extension);
        output = new BufferedWriter(new FileWriter(sqlFile));
        output.write(text);
    } catch (IOException e) {
        e.printStackTrace();
        logger.severe("Unable to write to file!\n");
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
                logger.severe("Unable to close buffer\n");
            }
        }
    }
}
</code></pre>

<p>And this a the part of my <code>run()</code> method, which uses the code above:</p>

<pre><code>    ClassLoader loader = this.getClass().getClassLoader();
    InputStream createTableInputStream = loader.getResourceAsStream("val_table_create.sql");

    if (createTableInputStream == null) {
        logger.severe("No tempalte found for creating table!\n");
        return;
    }

    List&lt;Sheet&gt; bookSheets = getSheets(book);

    for (Sheet sheet : bookSheets) {
        setHeader(table, sheet);

        String exportText = getString(createTableInputStream);
        exportText = exportText.replaceAll(TABLE_NAME, tableName);
        // exportText = exportText.replaceAll(VAL_DATA_TYPE, valDataType);
        // exportText = exportText.replaceAll(MSG_TEXT_DATA_TYPE, messageDataType);
        exportFile(exportText, absoluteWorkspacePath + File.separator + outputPath + File.separator, tableName, ".sql");
    }

    if (createTableInputStream != null) {
        createTableInputStream.close();
    }
</code></pre>