Java FileReader Create readTextFile(String fileName, String enc)

Here you can find the source of readTextFile(String fileName, String enc)

Description

Reads a character file into a string.

License

Open Source License

Parameter

Parameter Description
fileName The name of the file to read.

Return

The read file

Declaration

public static String readTextFile(String fileName, String enc) 

Method Source Code


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

import java.io.*;

public class Main {
    /** Reads a character file into a string. Assumes default
     *  character encoding in the file. Returns <code>null</code> if
     *  any of the exceptions is thrown.
     */*from w ww.j a  v  a  2s.  c o m*/
     *  @param fileName The name of the file to read.
     *  @return The read file
     */
    public static String readTextFile(String fileName) {
        String lineSep = System.getProperty("line.separator");
        StringBuffer retVal = new StringBuffer();
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            String line = null;
            while ((line = in.readLine()) != null) {
                retVal.append(line);
                retVal.append(lineSep);
            }
            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return retVal.toString();
    }

    /** Reads a character file into a string. Assumes default
     *  character encoding in the file. Returns <code>null</code> if
     *  any of the exceptions is thrown.
     *
     *  @param file The file to read.
     *  @return The read file
     */
    public static String readTextFile(File file) {
        String lineSep = System.getProperty("line.separator");
        StringBuffer retVal = new StringBuffer();
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = in.readLine()) != null) {
                retVal.append(line);
                retVal.append(lineSep);
            }
            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return retVal.toString();
    }

    /** Reads a character file into a string. Uses the specified
     *  character encoding. Returns <code>null</code> if
     *  any of the exceptions is thrown.
     *
     *  @param fileName The name of the file to read.
     *  @return The read file
     */
    public static String readTextFile(String fileName, String enc) {
        String lineSep = System.getProperty("line.separator");
        StringBuffer retVal = new StringBuffer();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), enc));
            String line = null;
            while ((line = in.readLine()) != null) {
                retVal.append(line);
                retVal.append(lineSep);
            }
            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return retVal.toString();
    }

    /** Reads a character file into a string. Uses the specified
     *  character encoding. Returns <code>null</code> if
     *  any of the exceptions is thrown.
     *
     *  @param fileName The name of the file to read.
     *  @return The read file
     */
    public static String readTextFile(File file, String enc) {
        String lineSep = System.getProperty("line.separator");
        StringBuffer retVal = new StringBuffer();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), enc));
            String line = null;
            while ((line = in.readLine()) != null) {
                retVal.append(line);
                retVal.append(lineSep);
            }
            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return retVal.toString();
    }
}

Related

  1. readTextFile(String fileName)
  2. readTextFile(String fileName)
  3. readTextFile(String filename)
  4. readTextFile(String filename)
  5. readTextFile(String fileName, boolean newline)
  6. readTextFile(String filePath)
  7. readTextFile(String filePath)
  8. readTextFile(String filePath)
  9. readTextFile(String fullPathFilename)