Java File to String readFileAsString(String filePath)

Here you can find the source of readFileAsString(String filePath)

Description

File as string [See also MixupUtil.fileToText(File file), which return the file's content and subject line as a concat.

License

Open Source License

Parameter

Parameter Description
filePath a parameter

Declaration

public static String readFileAsString(String filePath) 

Method Source Code


//package com.java2s;
/*//from  w w  w.j  a  v a2s  .c  om
 * 
 * Mole - Mobile Organic Localisation Engine
 * Copyright (C) 2010 Nokia Corporation.  All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.io.BufferedReader;
import java.io.FileReader;

public class Main {
    /**
     * File as string 
     * [See also MixupUtil.fileToText(File file), which return the file's content
     * and subject line as a concat. string.]
     * @param filePath
     * @return
     */
    public static String readFileAsString(String filePath) {
        try {
            StringBuffer fileData = new StringBuffer(1000);
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            char[] buf = new char[1024];
            int numRead = 0;
            while ((numRead = reader.read(buf)) != -1) {
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
            return fileData.toString();
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. fileToStringList(String filePath)
  2. fileToStringOneLine(String path)
  3. readFileAsString(String filePath)
  4. readFileAsString(String filePath)
  5. readFileAsString(String filePath)
  6. readFileAsString(String filePath)
  7. readFileAsString(String filePath)
  8. readFileAsString(String filePathname)
  9. readFileAsString(String fname)