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) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  www .  ja v  a  2  s.  co m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

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

public class Main {
    public static String fileToString(String fileName) throws IOException {
        System.gc();
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        //BufferedInputStream bis = null;
        BufferedReader dis = new BufferedReader(new InputStreamReader(fis));
        // dis.available() returns 0 if the file does not have more lines.
        StringBuffer buff = new StringBuffer();
        String line = null;
        while ((line = dis.readLine()) != null) {
            buff.append(line);
        }
        // dispose all the resources after using them.
        fis.close();
        dis.close();
        return buff.toString();
    }
}

Related

  1. fileToString(String file, String encoding)
  2. FileToString(String file_name)
  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)