Java BufferedReader Read readFile(String fileName)

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

Description

Read contents of a file into a string.

License

Apache License

Parameter

Parameter Description
file a parameter

Return

file contents.

Declaration

public static String readFile(String fileName) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.FileReader;

public class Main {
    /**/*from   w  w  w .j  av a 2 s.c  om*/
     * Read contents of a file into a string.
     * @param file
     * @return file contents.
     */
    public static String readFile(String fileName) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append('\n');
            }
            br.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

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)