Java BufferedReader Read readFile(String file)

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

Description

read File

License

Open Source License

Parameter

Parameter Description
file : It is the full path to the file

Return

String with the content of the file

Declaration

public static String readFile(String file) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**/*from   w  ww  .  j a v a 2s .c o  m*/
     * 
     * @param file
     *            : It is the full path to the file
     * 
     * @return String with the content of the file
     */
    public static String readFile(String file) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("Can't find the file", e);
        }
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");
        try {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            reader = null;
        }
    }
}

Related

  1. readFile(Reader reader)
  2. readFile(String absoluteFilePath)
  3. readFile(String aFileName)
  4. readFile(String directory)
  5. readFile(String directory, String fileName)
  6. readFile(String file)
  7. readFile(String file)
  8. readFile(String file)
  9. readFile(String file)