Here you can find the source of readFile(String filePath)
Parameter | Description |
---|---|
filePath | absolute path to file to be read |
Parameter | Description |
---|---|
IOException | an exception |
public static LinkedList<String> readFile(String filePath) throws IOException
//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(); } }