Java Text File Load loadText(File file, int bufsize)

Here you can find the source of loadText(File file, int bufsize)

Description

Takes the contents of a text file and returns it as a StringBuffer.

License

Mozilla Public License

Parameter

Parameter Description
file The file to copy text from.
bufsize The size of the buffer to allocate for copying.

Return

A new StringBuffer containing the contents of the specified text file.

Declaration

public static StringBuffer loadText(File file, int bufsize) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w ww  .  j  a  v  a2 s.  co m
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */

import java.io.*;

public class Main {
    /**
     * The default buffer size to use for copying, if none is specified.
     */
    public static int DEFAULT_BUFSIZE = 4096;

    /**
     * Takes the contents of a text file and returns it as a <CODE>StringBuffer</CODE>.
     *
     * @param file The file to copy text from.
     * @param bufsize The size of the buffer to allocate for copying.
     * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified text file.
     * @exception java.io.IOException If an exception occurred while reading data.
     * @see #load(java.io.Reader,int)
     */
    public static StringBuffer loadText(File file, int bufsize) throws IOException {
        FileReader rdr = new FileReader(file);
        try { // load from the string reader
            return load(rdr, bufsize);

        } // end try
        finally { // make sure and close the reader before we go
            shutdown(rdr);

        } // end finally

    }

    /**
     * Takes the contents of a text file and returns it as a <CODE>StringBuffer</CODE>.
     *
     * @param file The file to copy text from.
     * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified text file.
     * @exception java.io.IOException If an exception occurred while reading data.
     * @see #DEFAULT_BUFSIZE
     * @see #loadText(java.io.File,int)
     */
    public static StringBuffer loadText(File file) throws IOException {
        return loadText(file, DEFAULT_BUFSIZE);

    }

    /**
     * Takes the contents of an input stream and returns it as an array of bytes.
     *
     * @param input The stream to load binary data from.
     * @param bufsize The size of the buffer to allocate for copying.
     * @return A new byte array containing the contents of the specified input stream.
     * @exception java.io.IOException If an exception occurred while reading data.
     * @see #copy(java.io.InputStream,java.io.OutputStream,int)
     */
    public static byte[] load(InputStream input, int bufsize) throws IOException {
        ByteArrayOutputStream stm = new ByteArrayOutputStream();
        try { // copy the data to the input stream
            copy(input, stm, bufsize);
            return stm.toByteArray();

        } // end try
        finally { // close our byte array stream before we go
            shutdown(stm);

        } // end finally

    }

    /**
     * Takes the contents of an input stream and returns it as an array of bytes.  Uses a default
     * buffer size.
     *
     * @param input The stream to load binary data from.
     * @return A new byte array containing the contents of the specified input stream.
     * @exception java.io.IOException If an exception occurred while reading data.
     * @see #DEFAULT_BUFSIZE
     * @see #load(java.io.InputStream,int)
     */
    public static byte[] load(InputStream input) throws IOException {
        return load(input, DEFAULT_BUFSIZE);

    }

    /**
     * Takes the contents of an input reader and returns it as a <CODE>StringBuffer</CODE>.
     *
     * @param input The reader to copy character data from.
     * @param bufsize The size of the buffer to allocate for copying.
     * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified reader.
     * @exception java.io.IOException If an exception occurred while reading data.
     * @see #copy(java.io.Reader,java.io.Writer,int)
     */
    public static StringBuffer load(Reader input, int bufsize) throws IOException {
        StringWriter wr = new StringWriter();
        try { // copy from reader to StringWriter
            copy(input, wr, bufsize);
            return wr.getBuffer();

        } // end try
        finally { // make sure and close the StringWriter before we go
            shutdown(wr);

        } // end finally

    }

    /**
     * Takes the contents of an input reader and returns it as a <CODE>StringBuffer</CODE>.  Uses a
     * default buffer size.
     *
     * @param input The reader to copy character data from.
     * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified reader.
     * @exception java.io.IOException If an exception occurred while reading data.
     * @see #DEFAULT_BUFSIZE
     * @see #load(java.io.Reader,int)
     */
    public static StringBuffer load(Reader input) throws IOException {
        return load(input, DEFAULT_BUFSIZE);

    }

    /**
     * Closes an input stream cleanly, without throwing an exception.
     *
     * @param stm The stream to be closed.
     * @see java.io.InputStream#close()
     */
    public static void shutdown(InputStream stm) {
        try { // close the stream
            stm.close();

        } // end try
        catch (IOException e) { // throw away the exception
        } // end catch

    }

    /**
     * Closes an output stream cleanly, without throwing an exception.
     *
     * @param stm The stream to be closed.
     * @see java.io.OutputStream#close()
     */
    public static void shutdown(OutputStream stm) {
        try { // close the stream
            stm.close();

        } // end try
        catch (IOException e) { // throw away the exception
        } // end catch

    }

    /**
     * Closes an input reader cleanly, without throwing an exception.
     *
     * @param stm The stream to be closed.
     * @see java.io.Reader#close()
     */
    public static void shutdown(Reader rdr) {
        try { // close the stream
            rdr.close();

        } // end try
        catch (IOException e) { // throw away the exception
        } // end catch

    }

    /**
     * Closes an output reader cleanly, without throwing an exception.
     *
     * @param stm The stream to be closed.
     * @see java.io.Writer#close()
     */
    public static void shutdown(Writer wr) {
        try { // close the stream
            wr.close();

        } // end try
        catch (IOException e) { // throw away the exception
        } // end catch

    }

    /**
     * Copies the contents of the given input stream to the given output stream.
     *
     * @param input The stream to copy binary data from.
     * @param output The stream to copy binary data to.
     * @param bufsize The size of the buffer to allocate for copying.
     * @exception java.io.IOException If an exception occurred while reading or writing data.
     */
    public static void copy(InputStream input, OutputStream output, int bufsize) throws IOException {
        byte[] buffer = new byte[bufsize];
        int rd = input.read(buffer);
        while (rd >= 0) { // simple read-write loop to shove data out the door
            if (rd > 0)
                output.write(buffer, 0, rd);
            rd = input.read(buffer);

        } // end while

    }

    /**
     * Copies the contents of the given input stream to the given output stream.  Uses a default
     * buffer size.
     *
     * @param input The stream to copy binary data from.
     * @param output The stream to copy binary data to.
     * @exception java.io.IOException If an exception occurred while reading or writing data.
     * @see #DEFAULT_BUFSIZE
     * @see #copy(java.io.InputStream,java.io.OutputStream,int)
     */
    public static void copy(InputStream input, OutputStream output) throws IOException {
        copy(input, output, DEFAULT_BUFSIZE);

    }

    /**
     * Copies the contents of the given input reader to the given output writer.
     *
     * @param input The reader to copy character data from.
     * @param output The writer to copy character data to.
     * @param bufsize The size of the buffer to allocate for copying.
     * @exception java.io.IOException If an exception occurred while reading or writing data.
     */
    public static void copy(Reader input, Writer output, int bufsize) throws IOException {
        char[] buffer = new char[bufsize];
        int rd = input.read(buffer);
        while (rd >= 0) { // simple read-write loop to shove data out the door
            if (rd > 0)
                output.write(buffer, 0, rd);
            rd = input.read(buffer);

        } // end while

    }

    /**
     * Copies the contents of the given input reader to the given output writer.  Uses a default
     * buffer size.
     *
     * @param input The reader to copy character data from.
     * @param output The writer to copy character data to.
     * @exception java.io.IOException If an exception occurred while reading or writing data.
     * @see #DEFAULT_BUFSIZE
     * @see #copy(java.io.Reader,java.io.Writer,int)
     */
    public static void copy(Reader input, Writer output) throws IOException {
        copy(input, output, DEFAULT_BUFSIZE);

    }
}

Related

  1. loadText(File f)
  2. loadText(File file)
  3. loadText(File file)
  4. loadText(File file)
  5. loadText(java.io.File f)
  6. loadText(String fname)
  7. loadText(String path)
  8. loadText(String path)