Java BufferedReader Read readFile(String fileName)

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

Description

read File

License

Apache License

Declaration

@SuppressWarnings("resource")
    public static String readFile(String fileName) 

Method Source Code


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

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

public class Main {
    @SuppressWarnings("resource")
    public static String readFile(String fileName) {
        try {//www .  j a v a  2s . c  om
            File f = new File(fileName);
            if (!f.exists() || f.isDirectory())
                throw new IOException("File does not exist! " + f);

            StringBuilder model = new StringBuilder(100);
            BufferedReader bfr = null;

            bfr = new BufferedReader(new FileReader(f));
            String line = null;
            while ((line = bfr.readLine()) != null) {
                model.append(line + "\n");
            }
            model.delete(model.length() - 1, model.length());
            return model.toString();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        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, boolean print)