Java - user-mapped section open error
<p>When I try to write to the file specified it comes up with the error below. I have tried closing the FileInputStream but I still come up with the same problem. </p>

<p>Here is the relevant code:</p>

<p>Error log:</p>

<blockquote>
  <p>Error: C:\Path\Hours Log.csv (The requested operation cannot be performed on a file with a user-mapped section open)</p>
</blockquote>

<p>Code:</p>

<p>Creating the log:</p>

<pre><code>private void writeLog() throws IOException{

    //set up vars and write directories
    File yearStatDir = new File("C:\\Path);
    File yearStatPath = new File(yearStatDir + "\\" + "Hours Log.csv");
    String yearStatString = yearStatPath.toString();

    //read the files
    String existingYearLog = readLogFile(yearStatString, yearStatPath);

    //write the updated file
    String hoursString = "1";
    String dataYear = existingYearLog + hoursString;
    String folderYear = "Satistics\\Yearly data\\" + yearString;
    writeFile(dataYear, ".csv", folderYear, "Hours Log");
}
</code></pre>

<p>Writing the file:</p>

<pre><code>private void writeFile(String data, String fileType, String folder, String fileName){
    try{
        File fileDir = new File("C:\\Path\\" + folder);
        File filePath = new File(fileDir + "\\"+ fileName + fileType);
        writeDir(fileDir);
        // Create file 
        FileWriter fstream = new FileWriter(filePath);
        try (BufferedWriter out = new BufferedWriter(fstream)) {
            out.write(data);                        
            }
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
</code></pre>

<p>Reading the file:</p>

<pre><code>private static String readLogFile(String path, File f) throws IOException {
    if (f.exists()){
        try (FileInputStream stream = new FileInputStream(new File(path))) {
            FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    /* Instead of using default, pass in a decoder. */
    fc.close();
    return Charset.defaultCharset().decode(bb).toString();

        }
    }
    else {
        return "";
    }
}  
</code></pre>
