Java File Read via ByteBuffer readFile(String filename)

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

Description

read File

License

Apache License

Declaration

public static String readFile(String filename) 

Method Source Code

//package com.java2s;
/*//ww w  .java  2 s  .  c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

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

import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    public static String readFile(InputStream inputStream) {
        if (inputStream != null) {
            byte[] bytes = getBytes(inputStream);
            return new String(bytes);
        }
        return null;
    }

    public static String readFile(String filename) {
        if (filename != null) {
            byte[] bytes = getBytes(filename);
            return new String(bytes);
        }
        return null;
    }

    public static String readFile(File file) {
        final StringBuilder contents = new StringBuilder();
        try {
            final BufferedReader input = new BufferedReader(new FileReader(file));
            try {
                String line = null;
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        return contents.toString();
    }

    public static byte[] getBytes(File file) {
        try {
            return getBytes(new FileInputStream(file));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static byte[] getBytes(InputStream inputStream) {
        if (inputStream == null) {
            return null;
        }
        ByteArrayOutputStream output = null;
        try {
            ByteBuffer buffer = ByteBuffer.allocate(8192);
            ReadableByteChannel readChannel = Channels.newChannel(inputStream);
            output = new ByteArrayOutputStream(32 * 1024);
            WritableByteChannel writeChannel = Channels.newChannel(output);
            while ((readChannel.read(buffer)) > 0 || buffer.position() != 0) {
                buffer.flip();
                writeChannel.write(buffer);
                buffer.compact();
            }
            return output.toByteArray();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            if (output != null) {
                try {
                    output.close();
                    output = null;
                } catch (IOException ex) {
                }
            }
        }
    }

    public static byte[] getBytes(String filename) {
        filename = getJavaFileSystemPath(filename);
        File file = new File(filename);
        return getBytes(file);
    }

    public static String getJavaFileSystemPath(String path) {
        if (path == null) {
            return null;
        }
        path = path.replace('\\', '/');
        return path;
    }
}

Related

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