Java File Read getFileContent(final String p_filename)

Here you can find the source of getFileContent(final String p_filename)

Description

get File Content

License

Open Source License

Declaration

public static String getFileContent(final String p_filename) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Lab-STICC Universite de Bretagne Sud, Lorient.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the CeCILL-B license available
 * at :// w w  w  .  jav a  2  s.c  o  m
 * en : http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
 * fr : http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
 * 
 * Contributors:
 * Dominique BLOUIN (Lab-STICC UBS), dominique.blouin@univ-ubs.fr
 ******************************************************************************/

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class Main {
    public static String getFileContent(final String p_filename) throws IOException {
        BufferedReader reader = null;

        try {
            final int size = 1024;
            reader = new BufferedReader(new FileReader(p_filename), size);
            char[] buffer = new char[1];
            final StringBuffer content = new StringBuffer();

            while (reader.read(buffer) != -1) {
                content.append(buffer);
            }

            return content.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    public static String getFileContent(final String p_filename, final int pi_startPos, final int pi_length)
            throws IOException {
        return getContent(new FileInputStream(p_filename), pi_startPos, pi_length);
    }

    public static void read(final InputStreamReader p_reader, final char[] pc_bytes, int pi_length)
            throws IOException {
        int bufferOffset = 0;
        int charactersRead = 0;

        while (charactersRead >= 0 && pi_length > 0) {
            charactersRead = p_reader.read(pc_bytes, bufferOffset, pi_length);

            if (charactersRead > 0) {
                bufferOffset += charactersRead;
                pi_length -= charactersRead;
            }
        }
    }

    public static String getContent(final InputStream p_inputStream) throws IOException {
        final Reader reader = new InputStreamReader(p_inputStream);

        try {
            char[] buffer = new char[1];
            final StringBuffer content = new StringBuffer();

            while (reader.read(buffer) != -1) {
                content.append(buffer);
            }

            return content.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    public static String getContent(final InputStream p_inputStream, final int pi_startPos, final int pi_length)
            throws IOException {
        InputStreamReader reader = null;

        try {
            reader = new InputStreamReader(p_inputStream);
            char[] bytes = new char[pi_length];

            skip(reader, pi_startPos);

            read(reader, bytes, pi_length);

            return new String(bytes);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    public static void skip(final InputStreamReader p_reader, int pi_startPos) throws IOException {
        long skipped = 0;

        while (skipped >= 0 && pi_startPos > 0 && p_reader.ready()) {
            skipped = p_reader.skip(pi_startPos);

            if (skipped > 0) {
                pi_startPos -= skipped;
            }
        }
    }
}

Related

  1. getFileContent(File pFile)
  2. getFileContent(final File f)
  3. getFileContent(final File file)
  4. getFileContent(final File srcFile)
  5. getFileContent(final String fileName)
  6. getFileContentAsAString(final File aFile)
  7. getFileContentAsList(String filePath)
  8. getFileContentByLine(String filePath)
  9. getFileContentFromClasspath(String pathToFile)