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 6662629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:24:36+00:00 2026-05-26T02:24:36+00:00

Here is a code snippet where I am opening a file for writing with

  • 0

Here is a code snippet where I am opening a file for writing with two FileOutputStreams at the same time.

FileOutputStream fis = null;
File openFile = new File("myfile");
try {
    fis = new FileOutputStream(openFile);
    Toast toast = Toast.makeText(getApplicationContext(), "Opened", Toast.LENGTH_SHORT);
    toast.show();
}
catch (FileNotFoundException e1) {
    Toast toast = Toast.makeText(getApplicationContext(), "FileNotFound", Toast.LENGTH_SHORT);
    toast.show();
}

// now try to open it again
FileOutputStream fis2 = null;
try {
    fis2 = new FileOutputStream(openFile);
    Toast toast = Toast.makeText(getApplicationContext(), "Opened2", Toast.LENGTH_SHORT);
    toast.show();
}
catch (Exception e1) {
    Toast toast = Toast.makeText(getApplicationContext(), "Exception: " + e1.getMessage(), Toast.LENGTH_SHORT);
    toast.show();
}

I end up getting two Toast messages, “Opened” and “Opened2”.

I need to make sure I don’t open a file for reading/writing/deleting that is currently open for writing by another instance. How to I ensure I do not modify a file that is currently open for writing?

  • 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-26T02:24:36+00:00Added an answer on May 26, 2026 at 2:24 am

    I need to make sure I don’t open a file for reading/writing/deleting
    that is currently open for writing by another instance.

    What do you mean by another instance?

    • If this is another thread in your application use the synchronization for read/write methods.
    • If this is another process created by yourself (application, service etc.) use exclusive FileLock to coordinate file read/write operations between processes.
    • If this is another process you don’t control (i.e. created by someone else) you can check whether file is currently opened by inspecting /proc/…/fd/ tree, as lsof utility does. However this usually requires you to have root privileges.

    Hope that give you a clue. Let me know if you need further assistance.

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

Sidebar

Related Questions

Here is the code snippet. read = new FileReader(trainfiles/+filenames[i]); br = new BufferedReader(read); while((lines
Here is a code snippet for an asp:Wizard control I have on an aspx
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Replaces Question: Update multiple rows into SQL table Here's a Code Snippet to update
Using the obsolete System.Web.Mail sending email works fine, here's the code snippet: Public Shared
Here is a VB.NET code snippet Public Class OOPDemo Private _strtString as String Public
here is the relevent code snippet: public static Rand searchCount (int[] x) { int
Here's the relevant code snippet. public static Territory[] assignTerri (Territory[] board, String[] colors) {
I saw a code snippet yesterday in one of the responses here on StackOverflow
Here's a snippet of code from within TurboGears 1.0.6: [dispatch.generic(MultiorderGenericFunction)] def run_with_transaction(func, *args, **kw):

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.