Java File Read via ByteBuffer readFile(String filename)

Here you can find the source of readFile(String filename)

Description

read File

License

Open Source License

Declaration

public static String readFile(String filename) 

Method Source Code

//package com.java2s;
/*/* www .ja  v a 2  s  . c  om*/
 *  Copyright (c) 2009, Amostudio,inc
 *  All rights reserved.
 *  Code licensed under the BSD License:
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Amostudio,inc  nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY Amostudio,inc ''AS IS'' AND ANY
 *   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL Amostudio,inc  BE LIABLE FOR ANY
 *   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static String readFile(String filename) {
        return readFile(filename, false);
    }

    public static String readFile(String filename, boolean useNIO) {
        String s = "";
        if (useNIO) {
            s = new String(largeFileReader(filename));
        } else {
            s = new String(smallFileReader(filename));
        }
        return s;
    }

    /**
     *
     * good for Large Files >2Mb
     * @param filename
     * @return
     */
    private static byte[] largeFileReader(String filename) {
        byte[] bytes = null;
        FileChannel fc = null;
        try {
            fc = new FileInputStream(filename).getChannel();

            MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            int size = byteBuffer.capacity();
            if (size > 0) {
                // Retrieve all bytes in the buffer
                byteBuffer.clear();
                bytes = new byte[size];
                byteBuffer.get(bytes, 0, bytes.length);
            }
            fc.close();
        } catch (FileNotFoundException fnf) {
            System.err.println("" + fnf);
        } catch (IOException io) {
            System.err.println("" + io);
        } finally {
            if (fc != null) {
                try {
                    fc.close();
                } catch (IOException ioe) {
                    // ignore
                }
            }
        }
        return bytes;
    }

    public static byte[] smallFileReader(String filename) {
        byte[] buffer = null;
        FileInputStream in = null;
        try {
            File file = new File(filename);
            int size = (int) file.length();
            if (size > 0) {
                buffer = new byte[size];
                in = new FileInputStream(file);
                in.read(buffer, 0, size);

                //s = new String(buffer, 0, size);
                in.close();
            }
        } catch (FileNotFoundException fnfx) {
            System.err.println("File not found: " + fnfx);
        } catch (IOException iox) {
            System.err.println("I/O problems: " + iox);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioe) {
                }
            }
        }
        return buffer;
    }
}

Related

  1. readFile(File file)
  2. readFile(java.io.File file)
  3. readFile(String filename)
  4. readFile(String filename)
  5. readFile(String fileName)
  6. readFile(String filePath)
  7. readFile(String name)
  8. readFile(String name)
  9. readFile(String path)