Java File Content Read getFileContents(File file)

Here you can find the source of getFileContents(File file)

Description

get File Contents

License

Open Source License

Declaration

public static String getFileContents(File file) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * 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
 * /*  w ww.j ava 2 s .  co m*/
 * Contributors:
 *     Dimitrios Kolovos - initial API and implementation
 ******************************************************************************/

import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;

import java.util.Collection;

import java.util.LinkedList;
import java.util.List;

public class Main {
    public static String getFileContents(File file) throws Exception {
        final StringBuffer buffer = new StringBuffer();
        final String lineSeparator = System.getProperty("line.separator");

        for (String line : getFileLineContents(file)) {
            buffer.append(line);
            buffer.append(lineSeparator);
        }

        return buffer.toString();
    }

    public static Collection<String> getFileLineContents(File file) throws Exception {
        final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        try {
            final List<String> lines = new LinkedList<String>();

            String line = bufferedReader.readLine();

            while (line != null) {
                lines.add(line);
                line = bufferedReader.readLine();
            }

            return lines;
        } finally {
            bufferedReader.close();
        }
    }
}

Related

  1. getFileContent(String path, String encoding)
  2. getFileContent(String pythonSource)
  3. getFileContents(File f)
  4. getFileContents(File f)
  5. getFileContents(File file)
  6. getFileContents(File methodPatch)
  7. getFileContents(File testFile)
  8. getFileContents(final File f)
  9. getFileContents(final File file)