Java Stream Close close(Object resource)

Here you can find the source of close(Object resource)

Description

Closes the streams and resources.

License

Open Source License

Parameter

Parameter Description
resource to be closed.

Declaration

public static void close(Object resource) 

Method Source Code

//package com.java2s;
/*/*  w  w  w  .  j av  a 2s  .c om*/
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

public class Main {
    /**
     * Closes the streams and resources.
     * 
     * @param resource
     *            to be closed.
     */
    public static void close(Object resource) {
        try {
            if (resource instanceof InputStream) {
                ((InputStream) resource).close();
            } else if (resource instanceof OutputStream) {
                ((OutputStream) resource).close();
            } else if (resource instanceof Reader) {
                ((Reader) resource).close();
            } else if (resource instanceof Writer) {
                ((Writer) resource).close();
            } else if (resource instanceof RandomAccessFile) {
                ((RandomAccessFile) resource).close();
            } else if (resource != null) {
                throw new IllegalArgumentException("Unknown resource: "
                        + resource);
            }
        } catch (IOException e) {
        }
    }
}

Related

  1. close(final Scanner scanner)
  2. close(Iterator iterator)
  3. close(java.io.Closeable in)
  4. close(JMXConnector connector)
  5. close(Object obj)
  6. close(ObjectInput in)
  7. close(OutputStream in)
  8. close(OutputStream os)
  9. close(OutputStream s)