Java FileOutputStream Create writeFile(String path, Properties store, String comment)

Here you can find the source of writeFile(String path, Properties store, String comment)

Description

Writes data of a Properties object to a properties file.

License

Apache License

Parameter

Parameter Description
path Path to the file.
store Contains the data.
comment Comment that should be written in the file. May be null.

Exception

Parameter Description
IOException If the method failes to save the properties, an IOException will be thrown.
NullPointerException If path or store are null, a NullPointerException will be thrown.

Declaration

public static void writeFile(String path, Properties store, String comment) throws IOException 

Method Source Code


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

import java.io.*;
import java.util.Properties;

public class Main {
    /**
     * Writes data of a Properties object to a properties file. If necessary, new directories will be created.
     *
     * @param path
     *          Path to the file.
     * @param store
     *          Contains the data.
     * @param comment
     *          Comment that should be written in the file. May be null.
     * @throws IOException
     *          If the method failes to save the properties, an IOException will be thrown.
     * @throws NullPointerException
     *          If path or store are null, a NullPointerException will be thrown.
     */
    public static void writeFile(String path, Properties store, String comment) throws IOException {
        checkNullReference(path, store);
        File file = new File(path);
        File topDir = file.getParentFile();
        if (topDir != null && !topDir.exists()) {
            topDir.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(file);
        store.store(fos, comment);
    }

    private static void checkNullReference(Object... objects) {
        for (Object o : objects) {
            if (o == null) {
                throw new NullPointerException("objects must not be null");
            }
        }
    }
}

Related

  1. writeFile(String filePath, Object obj)
  2. writeFile(String filePathName, Object obj)
  3. writeFile(String p_filename, String p_buffer)
  4. writeFile(String path, byte[] contents)
  5. writeFile(String path, byte[] data)
  6. writeFile(ZipEntry entry, ZipFile zipFile, File file)
  7. writeFile0(File file, CharSequence content, Iterable lines)
  8. writeFileAsBytes(String fullPath, byte[] bytes)
  9. writeFileAsString(final File filePath, final String output, final String charset)