Java Properties Save save(File file, Properties properties)

Here you can find the source of save(File file, Properties properties)

Description

Saves the given Properties to the given File .

License

Open Source License

Parameter

Parameter Description
file The file where the object should be saved.
properties The Properties object to save.

Exception

Parameter Description
IOException If method fails to open or write to the file.

Declaration

public static void save(File file, Properties properties) throws IOException 

Method Source Code

//package com.java2s;
/**//www .  j  a v a  2s .  c om
 * Copyright (c) 2013, Bj?rn Kahlert, Stephan Aiche.
 *
 * This file is part of GenericKnimeNodes.
 * 
 * GenericKnimeNodes 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
 * (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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Main {
    /**
     * Saves the given {@link Properties} to the given {@link File}. If
     * necessary the {@link File} is automatically created.
     * 
     * @param file
     *            The file where the object should be saved.
     * @param properties
     *            The {@link Properties} object to save.
     * @throws IOException
     *             If method fails to open or write to the file.
     */
    public static void save(File file, Properties properties) throws IOException {
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                boolean mkdirs = file.getParentFile().mkdirs();
                if (!mkdirs) {
                    throw new IOException(String.format("Failed to create parent directories of file %s",
                            file.getAbsolutePath()));
                }
            }
            boolean createFile = file.createNewFile();
            if (!createFile) {
                throw new IOException(String.format("Failed to create file %s", file.getAbsolutePath()));
            }
        }
        FileWriter f_writer = new FileWriter(file);
        try {
            properties.store(f_writer, null);
        } catch (IOException ex) {
            // close file writer and rethrow
            f_writer.close();
            throw ex;
        }
        f_writer.close();
    }
}

Related

  1. save()
  2. save()
  3. save(Properties properties, File propertiesFile)
  4. save(Properties props, File file, String comment)
  5. save(String fileName, CharSequence string)
  6. saveBuildConfiguration(IFile ifile)