Java Properties Load from File load(File file)

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

Description

Tries to load the file and return its content.

License

Open Source License

Parameter

Parameter Description
file the file to open

Return

the content, otherwise null

Declaration

public static String load(File file) 

Method Source Code

//package com.java2s;
/*/* ww w .ja v a2  s. c  o m*/
 *   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.FileReader;

public class Main {
    /**
     * Tries to load the file and return its content.
     * 
     * @param file   the file to open
     * @return      the content, otherwise null
     */
    public static String load(File file) {
        StringBuffer result;
        BufferedReader reader;
        String line;
        String newLine;

        result = new StringBuffer();
        newLine = System.getProperty("line.separator");
        reader = null;
        try {
            // add new content
            reader = new BufferedReader(new FileReader(file));
            while ((line = reader.readLine()) != null) {
                result.append(line);
                result.append(newLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    // ignored
                }
            }
        }

        return ((result != null) ? result.toString() : null);
    }
}

Related

  1. load(boolean loadAsResourceBundle, String name, ClassLoader loader)
  2. load(Class type, String resource, Properties map)
  3. load(Class cls)
  4. load(File f)
  5. load(File file)
  6. load(File file)
  7. load(File file)
  8. load(File file)
  9. load(File file, Properties data)