Java ClassLoader storePropertiesFile(String fileName, String filePath, String storeComment)

Here you can find the source of storePropertiesFile(String fileName, String filePath, String storeComment)

Description

store Properties File

License

Open Source License

Declaration

public static void storePropertiesFile(String fileName, String filePath, String storeComment) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileOutputStream;

import java.io.OutputStream;

import java.net.URL;

import java.util.Properties;

public class Main {
    private static Properties properties = new Properties();

    public static void storePropertiesFile(String fileName, String filePath, String storeComment) {
        if (fileName == null || "".equals(fileName.trim())) {
            throw new RuntimeException("File name is null");
        } else {/*w  w  w  .  j a va  2s  . co  m*/
            fileName = fileName.trim();
            if (!fileName.toLowerCase().endsWith(".properties")) {
                fileName += ".properties";
            }
        }
        File propertiesFile = null;
        OutputStream out;
        try {
            if (filePath == null || "".equals(filePath.trim())) {
                filePath = Thread.currentThread().getContextClassLoader().getResource("").toString() + fileName;
                propertiesFile = new File(new URL(filePath).toURI());
            } else {
                filePath = filePath.trim();
                if (!filePath.endsWith("/") && !filePath.endsWith("\\")) {
                    filePath += "/";
                }
                filePath += fileName;
                propertiesFile = new File(filePath);
            }
            out = new FileOutputStream(propertiesFile);
            properties.store(out, storeComment);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. readFromFile(Object obj, String fileName)
  2. readSampleJson(String name)
  3. saveProperties(Properties properties, String name)
  4. scanPackage(String path)
  5. searchByAttributeKey(String key)
  6. toTypeArray(String[] s)