Java File to String fileToString(String fileName)

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

Description

file To String

License

Open Source License

Declaration

public static String fileToString(String fileName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2013 Andrei Olaru, Marius-Tudor Benea, Nguyen Thi Thuy Nga, Amal El Fallah Seghrouchni, Cedric Herpson.
 * /*w  w w  . j  av  a  2  s .com*/
 * This file is part of tATAmI-PC.
 * 
 * tATAmI-PC is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
 * 
 * tATAmI-PC 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with tATAmI-PC.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;

import java.util.Scanner;

public class Main {
    public static String fileToString(String fileName) {
        String ret = null;
        try {
            InputStream in = new FileInputStream(fileName);
            ret = streamToString(in);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static String streamToString(InputStream stream) {
        String ret = "";
        Scanner scanner = null;
        scanner = new Scanner(stream);
        while (scanner.hasNextLine())
            ret += scanner.nextLine();
        return ret;
    }
}

Related

  1. fileToString(String fileName)
  2. fileToString(String fileName)
  3. fileToString(String fileName)
  4. fileToString(String filename)
  5. fileToString(String filename)
  6. fileToString(String fileName)
  7. fileToString(String filename)
  8. fileToString(String fileName)
  9. fileToString(String fileName)