Android UTF8 File Read readFromFile(String fileName)

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

Description

reads the contents of the file passed in and returns the content as a String.

License

Open Source License

Parameter

Parameter Description
fileName a parameter

Exception

Parameter Description
IOException an exception

Return

- string representation of the file

Declaration

public static String readFromFile(String fileName) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w  w w .ja  v  a 2 s .c o m*/
 *  Copyright (C) 2010-2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    /**
     * reads the contents of the file passed in and returns the content as a String. This assumes
     * that the file is encoded in UTF-8
     * 
     * @param fileName
     * @return - string representation of the file
     * @throws IOException
     */
    public static String readFromFile(String fileName) throws IOException {
        StringBuilder sb = new StringBuilder();
        FileInputStream fstream = new FileInputStream(fileName);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in,
                "UTF-8"));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }
}

Related

  1. loadUTF8(File file)
  2. loadUTF8LinesFromFile(File file)
  3. readFile(String fileName, String encoding)