Java FileInputStream Read readFile(String pPathToFile)

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

Description

Read a file and return the content.

License

Apache License

Parameter

Parameter Description
pPathToFile path to file to read.

Exception

Parameter Description
IOException an exception

Return

String contained in the document.

Declaration

public static String readFile(String pPathToFile) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

import java.io.IOException;

public class Main {
    /**// ww  w.jav a 2  s.c o  m
     * Read a file and return the content.
     * 
     * @param pPathToFile
     *            path to file to read.
     * @return String contained in the document.
     * @throws IOException
     */
    public static String readFile(String pPathToFile) throws IOException {
        StringBuffer out = new StringBuffer();
        FileInputStream inputStrem = new FileInputStream(new File(pPathToFile));
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        while ((len = inputStrem.read(buf)) > 0) {
            outStream.write(buf, 0, len);
            out.append(outStream.toString());
        }
        inputStrem.close();
        outStream.close();

        return out.toString();
    }
}

Related

  1. readFile(String path)
  2. readFile(String path, long offset)
  3. readFile(String path, Properties store)
  4. readFile(String path, String encoding)
  5. readFile(String path, String root, OutputStream out)
  6. readFile(String resource)
  7. readFile(String sFileName)
  8. readFileAsBinary(File file)
  9. readFileAsInputStream(File file)