Java InputStreamReader Read readFile(String filePath)

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

Description

Read file into list

License

Apache License

Parameter

Parameter Description
filePath absolute path to file to be read

Exception

Parameter Description
IOException an exception

Return

list with file content splited by new line

Declaration

public static LinkedList<String> readFile(String filePath)
        throws IOException 

Method Source Code

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

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

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

import java.util.LinkedList;

public class Main {
    /**/*  www  . j  a v a 2s  . c  o  m*/
     * Read file into list
     * @param filePath absolute path to file to be read
     * @return list with file content splited by new line
     * @throws IOException
     */
    public static LinkedList<String> readFile(String filePath)
            throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), "UTF-8"));
        return read(reader);
    }

    private static LinkedList<String> read(BufferedReader reader)
            throws IOException {
        LinkedList<String> list = new LinkedList<String>();
        String line;
        while ((line = reader.readLine()) != null) {
            list.add(line);
        }
        reader.close();
        return list;
    }

    /**
     * Read input stream into byte array
     * @param is input stream to read
     * @return byte array with input stream contents
     * @throws IOException
     */
    public static byte[] read(InputStream is) throws IOException {
        // read the response
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }

        buffer.flush();

        return buffer.toByteArray();
    }
}

Related

  1. readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)
  2. readFile(String filename1)
  3. readFile(String fileNm)
  4. readFile(String filePath)
  5. readFile(String filePath)
  6. readFile(String filePath)
  7. readFile(String filePath)
  8. readFile(String filePath)
  9. readFile(String filePath, String charsetName, long lineNow, long lineEnd)