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

  • Home
  • SEARCH
  • 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 6008633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:51:09+00:00 2026-05-23T01:51:09+00:00

Can we open multiple FileWriter stream to the same file at the same time.

  • 0

Can we open multiple FileWriter stream to the same file at the same time. I wrote some codes to test this, and apparently it allow to be happened. This stumble me. Since if I open a File Writer to file, and before close it, I try to delete the file, well I cant. So how and why can I open multiples FileWriter stream to the same file at one time? Here is what I try

private static final int SIZE = 1000;

public static void main(String[] args) throws IOException, InterruptedException {
    File file = new File("C:\\dev\\harry\\data.txt");
    FileWriter fileWriter = new FileWriter(file, true);
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    for (int i = 0; i < SIZE; i++) {
        bufferedWriter.write("1\n");
        Thread.sleep(100);
    }

    if (bufferedWriter != null) bufferedWriter.close();
    if (fileWriter != null) fileWriter.close();
}

I have another process, that does the exact same thing, but instead write 2 out, and I get both 1 and 2, in my data file.

  • 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-05-23T01:51:09+00:00Added an answer on May 23, 2026 at 1:51 am

    Can we open multiple FileWriter stream to the same file at the same time.

    Yes, it is quite possible.

    So how and why can I open multiples FileWriter stream to the same file at one time?

    You’ve already demonstrated how to open multiple FileWriter instances, so I’ll stick to why it is possible. In Java, all file or device based operations are typically dependent on the platform’s capabilities. You can consider java.io and other related packages to be thin wrappers around native code in the JVM that actually performs this functionality.

    Until Java 1.4 (when NIO came out), file locking was not possible in the Java, because the JVM did not make appropriate platform-specific system calls to lock files or ranges within files. This changed with NIO, which is available in the java.nio package. In the documentation of the FileChannel class, you’ll notice the following:

    In addition to the familiar read, write, and close operations of byte channels, this class defines the following file-specific operations:

    …

    A region of a file may be locked against access by other programs.

    This behavior, as you would have correctly guessed is due to the necessary platform-specific calls being made by the JVM. If the underlying platform does not support this, file locking will not occur.

    As to why the behavior exists with FileWriter, the reason is very simple. NIO is/was the set of new I/O classes, but it did not replace java.io. You could therefore continue to use the java.io classes like FileOutputStream and FileWriter, but you’ll never be able to have the files locked by the JVM for the duration of the write operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I open multiple Eclipse workspaces at the same time on the Mac?
Currently In my embedded linux I can not open multiple ALSA sound stream. What
How can i open file in python and write to it multiple times? I
I have a Windows Forms (.NET) application that can have multiple documents open simultaneously.
In flash you can open a file on the user's computer. In flash 9
If I can open a connection to an MS Access file in C#, how
I have a program that can open multiple forms, and when there are a
In Mobile Safari, you can open up multiple different windows, and navigate between them
i want to open multiple dialog boxes by using the same class on both
Greetings all, In my application,user can open several QWindows which share the same data.(save

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.