Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:05:55+00:00 2026-06-11T00:05:55+00:00

When I try to write to the file specified it comes up with the

  • 0

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.

Here is the relevant code:

Error log:

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

Code:

Creating the log:

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");
}

Writing the file:

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());
        }
    }

Reading the file:

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 "";
    }
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:05:56+00:00Added an answer on June 11, 2026 at 12:05 am

    For anyone that comes across this, here is the alternative code that I am using now:

    private static String readLogFile(String path) throws IOException {
        File f = new File(path);
        if(f.exists()) {
            FileInputStream fis = new FileInputStream(f);
            Integer fileLength = (int) (long) f.length();
            byte[] b = new byte[fileLength];
            int read = 0;
            while (read < b.length) {
                read += fis.read(b, read, b.length - read);
            }
            String text = new String(b);
            return text;
        } else {
            String text = "";
            return text;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to lock a file and write to it with the code below:
I could Open and Write to the excel file, but when I try to
I try to write a script to scp a file from local server A
When I try to write UTF-8 Strings into an XML file using DOMDocument it
When I try to open a file in write mode with the following code:
I try to copy/write pcm-bytes (from ALSA-buffer) from the kernel-space into a file within
Background We have been working very hard to try come up with solutions for
I have written the following code to write a file on my local file
I have a file on which I want to replace the dates but I
I have a code that creates file(s) in user-specified directory. User can point to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.