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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:22:23+00:00 2026-05-15T21:22:23+00:00

Possible Duplicate: “using” keyword in java I’m transitioning from C# to java, so please

  • 0

Possible Duplicate:
“using” keyword in java

I’m transitioning from C# to java, so please bear with me…

When reading a file in C#, you simply wrap it all in a big ‘using’ block, so that if you have an exception, the file will still be closed. Like so (maybe inaccurate but you get the idea):

using(FileStream fs = new FileStream("c:\\myfile.txt")) {
  // Any exceptions while reading the file here won't leave the file open
}

Is there a convenient equivalent in java 5 or 6? I get the impression that lately java has been ‘borrowing’ some of the syntactic sugar from c# (such as foreach) and so i wouldn’t be surprised if there’s a java equivalent of using these days.

Or do i just have to use a try..finally block? ‘Using’ is just so much nicer i think…

  • 1 1 Answer
  • 1 View
  • 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-15T21:22:24+00:00Added an answer on May 15, 2026 at 9:22 pm

    There’s no equivalent syntax sugar using statements in Java 5 or 6. However, a proposal for automatic resource management which adds a familiar Disposable interface seems to have been accepted for Java 7 . Until then, you have to manually code a try…finally.

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

Sidebar

Related Questions

Possible Duplicate: Create instance of “Class” Using “Reflection” in JavaScript Java has Class.forName(XYZClass) to
Possible Duplicate: Why shall I use the “using” keyword to access my base class
Possible Duplicate: Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”' I am using Eclipse.
Possible Duplicate: How to convert yyyy-MM-dd HH:mm:ss to “15th Apr 2010” using PHP I
Possible Duplicate: “On file dialog cancel” event in JavaScript Is there a listener for
Possible Duplicate: “Undefined reference to” template class constructor I've just started using templates, and
Possible Duplicate: Python analog of natsort function (sort a list using a “natural order”
Possible Duplicate: “No newline at end of file” compiler warning i am a Linux
Possible Duplicate: Detecting an “invalid date” Date instance in JavaScript I was using the
Possible Duplicate: “new” keyword in Scala Ive noticed that creating certain instances in Scala

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.