Java Properties Save save(Properties properties, File propertiesFile)

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

Description

Saves properties to a file.

License

Open Source License

Parameter

Parameter Description
properties to be saved
propertiesFile destination properties file

Declaration

public static void save(Properties properties, File propertiesFile) 

Method Source Code

//package com.java2s;
/*//from w  w w .  j  a  v  a 2 s.  c  om
 * Copyright (c) 2007-2013 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;
import java.util.Properties;
import static com.google.common.base.Preconditions.checkNotNull;

public class Main {
    /**
     * Saves properties to a file.
     *
     * @param properties     to be saved
     * @param propertiesFile destination properties file
     */
    public static void save(Properties properties, File propertiesFile) {
        OutputStream out = null;
        try {
            checkNotNull(propertiesFile).getParentFile().mkdirs();
            out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
            if (propertiesFile.getName().endsWith(".xml")) {
                properties.storeToXML(out, null);
            } else {
                properties.store(out, null);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

Related

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