Java BufferedReader Read readFile(String fileName)

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

Description

read File

License

Creative Commons License

Declaration

public static String readFile(String fileName) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import java.io.BufferedReader;
import java.io.File;

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

public class Main {
    public static String readFile(String fileName) throws IOException {
        if (!new File(fileName).exists()) {
            return "";
        }/*from w  w w  .ja  v a 2 s .c o  m*/
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }
            return sb.toString();
        } finally {
            br.close();
        }
    }
}

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)