Java Utililty Methods Text File Read

List of utility methods to do Text File Read

Description

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

Method

StringgetFileContentAsString(String path)
get File Content As String
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String data = new String();
try {
    String line;
    while ((line = br.readLine()) != null) {
        data = data.concat(line);
} finally {
...
StringgetFileContents(Reader reader)
get File Contents
StringBuilder sb = new StringBuilder();
for (int c = reader.read(); c >= 0; c = reader.read()) {
    sb.append((char) c);
return sb.toString();
StringgetFileContents(String filePath)
get File Contents
try (InputStream stream = new FileInputStream(filePath)) {
    return getTextStreamContents(stream);
} catch (final IOException ex) {
    throw new IllegalArgumentException(ex);
StringgetFileContents(String filePath)
get File Contents
File file = new File(filePath);
Scanner scanner = new Scanner(file);
if (!scanner.hasNext()) {
    return null;
return scanner.useDelimiter("\\Z").next();
ListgetFileContents(String filePath)
Reads the contents of the file at the given path, returning it line-by- line in a List.
File file = new File(filePath);
if (file.exists() && !file.isDirectory()) {
    try (Scanner fileScanner = new Scanner(file)) {
        List<String> lines = new ArrayList<>();
        while (fileScanner.hasNextLine()) {
            lines.add(fileScanner.nextLine());
        fileScanner.close();
...
StringgetFileContents(String filePath)
Fetch the entire contents of a text file, and return it in a String.
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
input = new BufferedReader(new FileReader(new File(filePath)));
String line = null; 
while ((line = input.readLine()) != null) {
    contents.append(line);
    contents.append(System.getProperty("line.separator"));
return contents.toString();
VectorgetFileContents(String path)
get File Contents
Vector<String> words = new Vector<String>();
try {
    FileInputStream fstream = new FileInputStream(path);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null) {
        words.add(strLine);
...
StringgetFileContents(String pathToFile)
FileUtils.getFileContents() Returns the content of a file as a String
BufferedReader br = null;
String content = "";
try {
    String sCurrentLine;
    br = new BufferedReader(new FileReader(pathToFile));
    while ((sCurrentLine = br.readLine()) != null) {
        content += sCurrentLine + "\r\n";
} finally {
    if (br != null)
        br.close();
return content;
StringgetFileContents(String pFileName)
Method that returns the file contents
String fileContents = null;
InputStream is = new FileInputStream(pFileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer xml = new StringBuffer();
String aLine = null;
while ((aLine = reader.readLine()) != null) {
    xml.append(aLine);
    xml.append("\n");
...
StringloadAsString(String location)
load As String
final StringBuilder result = new StringBuilder();
try {
    final BufferedReader reader = new BufferedReader(new FileReader(location));
    String buffer = "";
    while ((buffer = reader.readLine()) != null) {
        result.append(buffer).append("\n");
    reader.close();
...