Java Folder Read getFileString(File file)

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

Description

get File String

License

Open Source License

Declaration

public static String getFileString(File file) 

Method Source Code


//package com.java2s;
/*    //from   w w w. j  av  a2  s  .c  o  m
 * traffic-light-program uses evolutionary computing to control traffic lights
 * Copyright (C) 2011  Pedro Henrique Oliveira dos Santos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.IOException;

public class Main {
    public static String getFileString(File file) {
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return contents.toString();
    }
}

Related

  1. getFilesOfTypeInDirectory(File directory, String filetype)
  2. getFilesRegex(final File root, final String[] regex)
  3. getFilesStartingWith(File parentDir, String prefix)
  4. getFilesStartingWith(String dirName, String startsWith)
  5. getFileStatus(File file)
  6. getFileString(String filePath)
  7. getFileStringContent(String filename)
  8. GetFilesWithChildren(File root, ArrayList files)
  9. getFileSystemProperties(String path)