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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:00:46+00:00 2026-05-17T18:00:46+00:00

I know that if I do something like copyFromInToOut(new FileInputStream(f1), new FileOutputStream(f2)); System.gc(); It

  • 0

I know that if I do something like

copyFromInToOut(new FileInputStream(f1), new FileOutputStream(f2));
System.gc();

It will run the GC on those FileInputStreams, closing them. But if I do

copyFromInToOut(new BufferedInputStream(new FileInputStream(f1)), new BufferedOutputStream(new FileOutputStream(f2));
System.gc();

Is there any danger that the FileOutputStream will be GCed before the BufferedOutputStream, not causing the buffer to flush?
I can’t call flush, close, because that takes more steps than this. It would first involve declaring a bufferedinputstream, passing, then calling close. OR am I safe to do this?

  • 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-17T18:00:47+00:00Added an answer on May 17, 2026 at 6:00 pm

    Don’t call System.gc() explicitly. Don’t rely on finalizers to do anything. Especially if you don’t understand how garbage collection works. Explicit garbage collection requests can be ignored, and finalizers might never run.

    A well-written copyFromInToOut method for streams is likely to use its own buffer internally, so wrapping the output should be unnecessary.

    Declare variables for the FileInputStream and FileOutputStream, and invoke close() on each in a finally block:

    FileInputStream is = new FileInputStream(f1);
    try {
      FileOutputStream os = new FileOutputStream(f2);
      try {
        copyFromInToOut(is, os);
        os.flush();
      } finally {
        os.close();
      }
    } finally {
      is.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that writing something like ++a = a++; Is not only unreadable but
I know that if I do something like this: class Obj { public: int*
I know that I can do something like: public class AbstractDao<T extends Bean> {
I want to know that, is something like this, possible? : #test{ background-color: red;
Why can I not do something like the following when I know that the
I've done enough Googling to know that if I have something like class SubObject
I'm new to jQuery AJAX. I want to build bookmarking system something like Twitter
So I know that you can add an object in java to something through
I'm sure there's something obvious I'm missing here, but I know that popover controls
This is something that's I've wanted to know recently, mostly out of curiousity. I'm

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.