Java BufferedReader Read readFile(String fileName)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(String fileName) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    public static String readFile(String fileName) throws IOException {
        if (fileName == null)
            throw new IOException("filename is null");

        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);
        StringBuilder buffer = new StringBuilder();

        String nextLine;//from  w  w w  .  j av  a2  s . c  om
        while ((nextLine = br.readLine()) != null)
            buffer.append(nextLine).append("\n");

        //Remove final newline
        if (buffer.length() > 0 && buffer.charAt(buffer.length() - 1) == '\n')
            buffer.deleteCharAt(buffer.length() - 1);

        br.close();
        fr.close();

        return buffer.toString();
    }
}

Related

  1. readFile(String fileName)
  2. readFile(String filename)
  3. readFile(String filename)
  4. readFile(String fileName)
  5. readFile(String fileName)
  6. readFile(String filename)
  7. readFile(String fileName)
  8. readFile(String fileName)
  9. readFile(String fileName)