Here you can find the source of readTextFile(String fileName, String enc)
Parameter | Description |
---|---|
fileName | The name of the file to read. |
public static String readTextFile(String fileName, String enc)
//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(); } }