Java BufferedReader Read readFile(File file)

Here you can find the source of readFile(File file)

Description

This method read a whole file and return the content.

License

Open Source License

Exception

Parameter Description

Declaration

public static String readFile(File file) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    public static final String END_LINE = "\n";

    /**/*ww  w .ja va 2 s  .c om*/
     * This method read a whole file and return the content.
     * 
     * @throws java.io.IOException when an error occurs while reading the file.
     */
    public static String readFile(File file) throws IOException {
        if (!file.exists()) {
            throw new IllegalArgumentException("Illegal path: unexisting '" + file.getPath() + "'");
        }
        if (!file.isFile()) {
            throw new IllegalArgumentException("Illegal path: not a dfile");
        }
        FileReader fr = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        try {
            fr = new FileReader(file);
            reader = new BufferedReader(fr);

            String line = null;
            do {
                line = reader.readLine();
                if (line != null) {
                    buffer.append(line);
                    buffer.append(END_LINE);
                }
            } while (line != null);

        } finally {
            reader.close();
            fr.close();
        }
        return buffer.toString();
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)