Here you can find the source of readTextFile(File inputFile)
Parameter | Description |
---|---|
inputFile | File to read |
Parameter | Description |
---|---|
IOException | On i/o error |
public static String readTextFile(File inputFile) throws IOException
//package com.java2s; /*// ww w . j av a 2 s . c o m * Copyright 2007 skynamics AG * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Main { /** * Reads a text file stream. * * @param in Inputstream * @return The contents of the file.<br> * The lines will be separated by "\\n". * @throws IOException On i/o error */ public static String readTextFile(InputStream in) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { if (sb.length() > 0) { sb.append('\n'); } sb.append(line); } return sb.toString(); } /** * Reads a text file. * * @param inputFileName Name of the file to read * @return The contents of the file.<br> * The lines will be separated by "\\n". * @throws IOException On i/o error */ public static String readTextFile(String inputFileName) throws IOException { FileInputStream in = null; try { in = new FileInputStream(inputFileName); return readTextFile(in); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } } /** * Reads a text file. * * @param inputFile File to read * @return The contents of the file.<br> * The lines will be separated by "\\n". * @throws IOException On i/o error */ public static String readTextFile(File inputFile) throws IOException { FileInputStream in = null; try { in = new FileInputStream(inputFile); return readTextFile(in); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } } }