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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:04:18+00:00 2026-05-11T01:04:18+00:00

As most should know close() also closes any streams uses. This allows the follow

  • 0

As most should know close() also closes any streams uses.

This allows the follow code:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(...))); ... br.close(); 

This is nice, since we don’t need a reference to FileInputStream and remember to close it.

But does it also work for FileLocks?

final FileInputStream fis = new FileInputStream(new File('buffer.txt')); final FileChannel c = fis.getChannel(); final FileLock lock = c.lock(0L, Long.MAX_VALUE, true); final BufferedReader br = new BufferedReader(new InputStreamReader(fis));  try {     while(br.ready()) {         System.out.println(br.readLine());     } } finally {     br.close(); } 

I’ve tried this code and the lock is correctly released when br.close() is called, but is is safe to do so? The Closeable JavaDoc says, ‘Closes this stream and releases any system resources associated with it.’ Am I safe to assume that I am using close() as specified to release() the lock?

  • 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. 2026-05-11T01:04:18+00:00Added an answer on May 11, 2026 at 1:04 am

    According to the JavaDoc:

    It remains valid until the lock is released by invoking the release method, by closing the channel that was used to acquire it, or by the termination of the Java virtual machine, whichever comes first.

    And here are the contents of FileInputStream.close()

    public void close() throws IOException {     if (channel != null)         channel.close();     close0(); } 

    It looks like close on the stream closes the channel which releases the lock.

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

Sidebar

Related Questions

I know that most links should be left up to the end-user to decide
This should be simple, I need to stop any previous version of my program
If I know there is at most 1 such object in my DOM, should
What things should be kept most in mind when writing cross-platform applications in C?
What is the most important features a IOC container should contain? You can easily
When are custom Exception classes most-valuable? Are there cases when they should or should
Most of the implementations I find require a hardware instruction to do this. However
ok i know this is stupid but atleast im trying :) $result = shell_exec('C:/cygwin/bin/bash.exe
We all know that we should rather reuse a JDBC PreparedStatement than creating a
I admit, I'm new at this whole Android stuff. I am trying to make

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.