Java FileReader Create readTextFile(String file)

Here you can find the source of readTextFile(String file)

Description

Read the file into a String.

License

Open Source License

Parameter

Parameter Description
file - the file to be read

Exception

Parameter Description
IOException - when we can't read the file

Return

String with the content of the file

Declaration

public static String readTextFile(String file) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**//from   ww  w  .jav  a2s . c om
      * Read the file into a String.
      * @param file - the file to be read
      * @return String with the content of the file
      * @throws IOException - when we can't read the file
      */
    public static String readTextFile(String file) throws IOException {
        File rFile = new File(file);
        StringBuffer sb = new StringBuffer(1024);
        BufferedReader reader = new BufferedReader(new FileReader(rFile.getPath()));

        char[] chars = new char[1];
        try {
            while ((reader.read(chars)) > -1) {
                sb.append(String.valueOf(chars));
                chars = new char[1];
            }
        } finally {
            reader.close();
        }
        return sb.toString();
    }
}

Related

  1. readTextfile(final String filename)
  2. readTextFile(InputStream in)
  3. readTextFile(InputStream in)
  4. readTextFile(String completePath)
  5. readTextFile(String file)
  6. readTextFile(String file)
  7. readTextFile(String filename)
  8. readTextFile(String filename)
  9. readTextFile(String fileName)