Java SQL PreparedStatement getFileContent(final File file)

Here you can find the source of getFileContent(final File file)

Description

Fully reads the given file content and returns it (to be used be small files only).

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Return

full content of the file

Declaration

public static final String getFileContent(final File file) throws IOException 

Method Source Code


//package com.java2s;
/* **********************************************************************
/*
 * NOTE: This copyright does *not* cover user programs that use Hyperic
 * program services by normal system calls through the application
 * program interfaces provided as part of the Hyperic Plug-in Development
 * Kit or the Hyperic Client Development Kit - this is merely considered
 * normal use of the program, and does *not* fall under the heading of
 * "derived work"./* w  w w  . ja v  a  2 s  . co  m*/
 *
 * Copyright (C) [2004-2012], VMware, Inc.
 * This file is part of Hyperic.
 *
 * Hyperic is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    public static final int NOOP_INSTRUCTION_FLAG = 0;

    /**
     * Fully reads the given file content and returns it (to be used be small files only).
     * @param file 
     * @return full content of the file 
     * @throws IOException
     */
    public static final String getFileContent(final File file) throws IOException {
        FileInputStream fis = null;
        String content = null;
        try {
            fis = new FileInputStream(file);

            final byte arrBytes[] = new byte[fis.available()];
            fis.read(arrBytes);
            content = new String(arrBytes);
        } finally {
            close(fis);
        } //EO catch block 

        return content;
    }

    /**
     * Closes all formal argument objects with no special operations.<br/> 
     * Supported types:<br/>  
     * - {@link Connection}<br/>
     * - {@link PreparedStatement}<br/>
     * - {@link OutputStream}<br/>
     * - {@link InputStream}<br/>
     * <p> 
     * <b>Note:</b> Exceptions are silenced.
     * </p>
     * @param closeables list of closeables
     */
    public static final void close(final Object... closeables) {
        close(NOOP_INSTRUCTION_FLAG, closeables);
    }

    /**
     * Closes all formal argument objects with no special operations.<br/> 
     * Supported types:<br/>  
     * - {@link Connection}<br/>
     *   - Special Instructions:<br/> 
     *     - {@value #COMMIT_INSTRUCTION_FLAG}<br/>
     *     - {@value #ROLLBACK_INSTRUCTION_FLAG}<br/>
     * - {@link PreparedStatement}<br/>
     * - {@link OutputStream}<br/>
     * - {@link InputStream}<br/>
     * <p> 
     * <b>Note:</b> Exceptions are silenced.
     * </p>
     * @param closeables list of closeables
     */
    public static final void close(final int specialInstructionsMask, final Object... closeables) {
        for (Object closeable : closeables) {
            if (closeables == null)
                continue;
            try {
                if ((closeable instanceof Connection)) {
                    Connection conn = (Connection) closeable;
                    try {
                        if ((specialInstructionsMask & 0x4) == 4)
                            conn.commit();
                        else if ((specialInstructionsMask & 0x8) == 8)
                            conn.rollback();
                    } catch (Throwable t2) {
                        printStackTrace(t2);
                    } //EO inner catch block 

                    ((Connection) closeable).close();
                } else if ((closeable instanceof PreparedStatement)) {
                    ((PreparedStatement) closeable).close();
                } else if ((closeable instanceof ResultSet)) {
                    ((ResultSet) closeable).close();
                } else if ((closeable instanceof Writer)) {
                    final Writer writer = (Writer) closeable;
                    writer.flush();
                    writer.close();
                } else if ((closeable instanceof Reader)) {
                    ((Reader) closeable).close();
                } else if ((closeable instanceof OutputStream)) {
                    final OutputStream os = (OutputStream) closeable;
                    os.flush();
                    os.close();
                } else if ((closeable instanceof InputStream)) {
                    final InputStream is = (InputStream) closeable;
                    is.close();
                } //EO else if inputstream
                else if ((closeable instanceof Closeable)) {
                    ((Closeable) closeable).close();
                } //EO else if Closeable
            } catch (Throwable t) {
                printStackTrace(t);
            } //EO catch block 
        } //EO while there are more closeables 
    }

    /**
     * @param exception to print 
     */
    public static final void printStackTrace(final Throwable exception) {
        printStackTrace(exception, null);
    }

    /**
     * Prints the exception to the {@link System#err} stream with an optional prefixMessage<br/> 
     * <b>Note:</b> If {@link SQLException}, prints all its linked exceptions as well.  
     * @param exception to print 
     * @param prefixMessage optional message to output to {@link System#err} stream  
     */
    public static final void printStackTrace(final Throwable exception, final String prefixMessage) {
        if (prefixMessage != null)
            System.err.println(prefixMessage);
        if ((exception instanceof SQLException)) {
            SQLException sqle = (SQLException) exception;
            Throwable t = null;
            do {
                t = sqle;
                t.printStackTrace();
            } while (((sqle = sqle.getNextException()) != null) && (sqle != t));
        } else {
            exception.printStackTrace();
        } //EO if instanceof SQLException 
    }
}

Related

  1. fillInClause(PreparedStatement ps, int startIndex, Collection clauses, int sqlType)
  2. fillParameters(PreparedStatement stmt, List params)
  3. fillPreparedStatementParams(PreparedStatement ps, Object... obj)
  4. fillStatement(PreparedStatement ps,Object...params)
  5. fillStatement(PreparedStatement stmt, Object[] params)
  6. getGeneratedKey(PreparedStatement ps)
  7. getGenerateKey(PreparedStatement stmt)
  8. getIdentity(PreparedStatement stat)
  9. getLastId(PreparedStatement s)

  10. HOME | Copyright © www.java2s.com 2016