Android Text File Read readStringFromFile(String fileName)

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

Description

Reads the entire file and returns its content as a single String

License

Open Source License

Parameter

Parameter Description
fileName the file

Exception

Parameter Description
IOException an exception

Return

the file content as String

Declaration

public static String readStringFromFile(String fileName)
        throws IOException 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URL;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

public class Main{
    /**//from   w ww. j  a  va2 s .com
     * Reads the entire file and returns its content as a single {@link String}
     *
     * @param fileName the file
     * @return the file content as String
     * @throws IOException
     */
    public static String readStringFromFile(String fileName)
            throws IOException {
        return readStringFromBufferedReader(createBufferedUtf8Reader(fileName));
    }
    /**
     * Reads all content from the given {@link Reader} and returns it as a single {@link String}
     *
     * @param br the file
     * @return the file content as String
     * @throws IOException
     */
    private static String readStringFromBufferedReader(BufferedReader br)
            throws IOException {
        StringBuffer sbr = new StringBuffer();
        for (String ln = br.readLine(); ln != null; ln = br.readLine())
            sbr.append(ln + "\n");
        br.close();
        return sbr.toString();
    }
    /**
     * Opens a file given by a path and returns its {@link BufferedReader} using the
     * UTF-8 encoding
     *
     * @param path path to a file to be read
     * @return UTF8 BufferedReader of the file <tt>path</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(String path)
            throws IOException {
        return createBufferedUtf8Reader(new File(path));
    }
    /**
     * Opens a file given by a path and returns its {@link BufferedReader} using the
     * UTF-8 encoding
     *
     * @param file file to be read
     * @return UTF8 BufferedReader of the <tt>file</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(File file)
            throws IOException {
        return createBufferedUtf8Reader(new FileInputStream(file));
    }
    /**
     * Opens a URL and returns its {@link BufferedReader} using the UTF-8 encoding
     *
     * @param url to be read
     * @return UTF8 BufferedReader of the <tt>url</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(URL url)
            throws IOException {
        return createBufferedUtf8Reader(url.openStream());
    }
    /**
     * Creates a {@link BufferedReader} on the top of the given {@link InputStream} using the
     * UTF-8 encoding
     *
     * @param is file to be read
     * @return UTF8 BufferedReader of the <tt>file</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(InputStream is)
            throws IOException {
        return new BufferedReader(new InputStreamReader(is, "utf8"));
    }
}

Related

  1. readFree(File file)
  2. readFromFile(File file)
  3. readFull(String path)
  4. readLines(String file)
  5. readStringFromClasspath(String path, Class c)
  6. readText(String path)
  7. readTextFile(String filePath)
  8. readTextFile(String path)
  9. setFileResourceText(String path, String content)