Java Utililty Methods File Read

List of utility methods to do File Read

Description

The list of methods to do File Read are organized into topic(s).

Method

StringgetFileContent(File file, int bufferSize)
get the content of the file
StringBuffer strBuffer = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(file), bufferSize);
String tempStr = in.readLine();
strBuffer.append(tempStr);
while ((tempStr = in.readLine()) != null) {
    strBuffer.append("\n").append(tempStr);
in.close();
...
StringgetFileContent(File pFile)
Read the file and returns its content
String content = new String();
FileReader fileReader = null;
try {
    fileReader = new FileReader(pFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return null;
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
try {
    while ((line = bufferedReader.readLine()) != null) {
        content += line;
} catch (IOException e) {
    e.printStackTrace();
    return null;
try {
    fileReader.close();
} catch (IOException e) {
    e.printStackTrace();
return content;
byte[]getFileContent(final File f)
get File Content
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
final byte[] buffer = new byte[40960];
while (in.available() > 0) {
    final int s = in.read(buffer);
    out.write(buffer, 0, s);
in.close();
...
StringgetFileContent(final File file)
Get the content of a file.
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line = null;
    final StringBuilder sb = new StringBuilder();
    while ((line = br.readLine()) != null) {
        if (Thread.currentThread().isInterrupted()) {
            throw new InterruptedException();
        sb.append(line);
...
StringgetFileContent(final File srcFile)
Returns the content of a file as string.
if ((srcFile != null) && srcFile.exists() && srcFile.isFile()) {
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
    StringBuffer contentOfFile = new StringBuffer();
    String line;
    while ((line = br.readLine()) != null) {
        contentOfFile.append(line);
    return contentOfFile.toString();
...
StringBuildergetFileContent(final String fileName)
get File Content
final StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
    final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    if (is == null)
        throw new Exception("File '" + fileName + "' not found!");
    br = new BufferedReader(new InputStreamReader(is, "ISO-8859-15"));
    String nextLine = "";
...
StringgetFileContent(final String p_filename)
get File Content
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);
...
StringgetFileContentAsAString(final File aFile)
get File Content As A String
FileInputStream fis = new FileInputStream(aFile);
int x = fis.available();
byte b[] = new byte[x];
fis.read(b);
return new String(b);
ListgetFileContentAsList(String filePath)
get File Content As List
return Arrays.asList(getFileContent(filePath).split("\\n"));
ListgetFileContentByLine(String filePath)
get File Content By Line
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
    return null;
List<String> content = new ArrayList<String>();
try {
    FileInputStream fileInputStream = new FileInputStream(file);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
...