Java File Read getFileContentAsList(String filePath)

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

Description

get File Content As List

License

Apache License

Declaration

public static List<String> getFileContentAsList(String filePath)
        throws IOException 

Method Source Code

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

import java.io.*;

import java.util.Arrays;
import java.util.List;

public class Main {
    private final static String DEFAULT_CHARSET = "UTF-8";

    public static List<String> getFileContentAsList(String filePath)
            throws IOException {
        return Arrays.asList(getFileContent(filePath).split("\\n"));
    }//  w  w  w .j a v  a  2  s  .co m

    public static List<String> getFileContentAsList(File file)
            throws IOException {
        return getFileContentAsList(file, DEFAULT_CHARSET);
    }

    public static List<String> getFileContentAsList(String filePath,
            String encoding) throws IOException {
        return Arrays.asList(getFileContent(filePath, encoding)
                .split("\\n"));
    }

    public static List<String> getFileContentAsList(File file,
            String encoding) throws IOException {
        return Arrays.asList(getFileContent(new FileInputStream(file),
                encoding).split("\\n"));
    }

    public static String getFileContent(String filePath) throws IOException {
        return getFileContent(filePath, DEFAULT_CHARSET);
    }

    public static String getFileContent(String filePath, String encoding)
            throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), encoding));
        String content = getContent(buff);
        buff.close();
        return content;
    }

    public static String getFileContent(File file) throws IOException {
        return getFileContent(new FileInputStream(file));
    }

    public static String getFileContent(InputStream is) throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(is,
                "UTF-8"));
        String content = getContent(buff);
        buff.close();
        return content;
    }

    public static String getFileContent(InputStream is, String encoding)
            throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(is,
                encoding));
        String content = getContent(buff);
        buff.close();
        return content;
    }

    private static String getContent(BufferedReader buff)
            throws IOException {
        String line;
        StringBuffer content = new StringBuffer();
        while ((line = buff.readLine()) != null) {
            content.append('\n');
            content.append(line);
        }
        return content.substring(1).toString();
    }
}

Related

  1. getFileContent(final File file)
  2. getFileContent(final File srcFile)
  3. getFileContent(final String fileName)
  4. getFileContent(final String p_filename)
  5. getFileContentAsAString(final File aFile)
  6. getFileContentByLine(String filePath)
  7. getFileContentFromClasspath(String pathToFile)
  8. getFileContentIntoStrCategoriesDictionary( String fileName)
  9. getFileContentLength(String filename)