Java BufferedReader Read readFile(File file)

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

Description

create a String from given File

License

Apache License

Parameter

Parameter Description
file File for txt document

Return

Document

Declaration

public static String readFile(File file) 

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 {
    /****//from  ww  w.  jav a  2s.  co  m
    * create a String from given File
    * 
    * @param file
    *            File for txt document
    * @return Document
    */
    public static String readFile(File file) {

        StringBuffer buf = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            while ((tempString = reader.readLine()) != null) {
                buf.append(tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

        return buf.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)