Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Main {
    public static boolean clearProperty(String filePath, String fileName, String propertyName) {
        try {
            Properties p = loadPropertyInstance(filePath, fileName);
            p.setProperty(propertyName, "");
            String comment = propertyName;
            return storePropertyInstance(filePath, fileName, p, comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Properties loadPropertyInstance(String filePath, String fileName) {
        try {
            File d = new File(filePath);
            if (!d.exists()) {
                d.mkdirs();
            }
            File f = new File(d, fileName);
            if (!f.exists()) {
                f.createNewFile();
            }
            Properties p = new Properties();
            InputStream is = new FileInputStream(f);
            p.load(is);
            is.close();
            return p;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean setProperty(String filePath, String fileName, String propertyName, String propertyValue) {
        try {
            Properties p = loadPropertyInstance(filePath, fileName);
            p.setProperty(propertyName, propertyValue);
            String comment = "Update '" + propertyName + "' value";
            return storePropertyInstance(filePath, fileName, p, comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setProperty(String filePath, String fileName, Map<String, String> propertyMap) {
        try {
            Properties p = loadPropertyInstance(filePath, fileName);
            for (String name : propertyMap.keySet()) {
                p.setProperty(name, propertyMap.get(name));
            }
            String comment = "Update '" + propertyMap.keySet().toString() + "' value";
            return storePropertyInstance(filePath, fileName, p, comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setProperty(String filePath, String fileName, String... propertyArray) {
        if (propertyArray == null || propertyArray.length % 2 != 0) {
            throw new IllegalArgumentException("make sure 'propertyArray' argument is 'ket/value' pairs");
        }
        try {
            Properties p = loadPropertyInstance(filePath, fileName);
            for (int i = 0; i < propertyArray.length / 2; i++) {
                p.setProperty(propertyArray[i * 2], propertyArray[i * 2 + 1]);
            }
            String comment = "Update '" + propertyArray[0] + "..." + "' value";
            return storePropertyInstance(filePath, fileName, p, comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setProperty(String filePath, String fileName, String propertyName,
            List<String> propertyValueList) {
        try {
            Properties p = loadPropertyInstance(filePath, fileName);
            StringBuilder propertyValue = new StringBuilder();
            if (propertyValueList != null && propertyValueList.size() > 0) {
                for (String value : propertyValueList) {
                    propertyValue.append(
                            value.replaceAll("(\\\\)+$", "").replaceAll("\\\\", "\\\\\\\\").replaceAll(";", "\\\\;")
                                    + ";");
                }
            }
            p.setProperty(propertyName, propertyValue.toString());
            String comment = "Update '" + propertyName + "' value";
            return storePropertyInstance(filePath, fileName, p, comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setProperty(String filePath, String fileName, String propertyName,
            Map<String, String> propertyValueMap) {
        try {
            Properties p = loadPropertyInstance(filePath, fileName);
            StringBuilder propertyValue = new StringBuilder();
            if (propertyValueMap != null && propertyValueMap.size() > 0) {
                for (String key : propertyValueMap.keySet()) {
                    propertyValue.append(key.replaceAll("\\\\", "\\\\\\\\").replaceAll("(\\\\)+$", "")
                            .replaceAll("\\,", "\\\\,").replaceAll(";", "\\\\;") + ","
                            + propertyValueMap.get(key).replaceAll("(\\\\)+$", "").replaceAll("\\\\", "\\\\\\\\")
                                    .replaceAll("\\,", "\\\\,").replaceAll(";", "\\\\;")
                            + ";");
                }
            }
            p.setProperty(propertyName, propertyValue.toString());
            String comment = "Update '" + propertyName + "' value";
            return storePropertyInstance(filePath, fileName, p, comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean storePropertyInstance(String filePath, String fileName, Properties p, String comment) {
        try {
            File d = new File(filePath);
            if (!d.exists()) {
                d.mkdirs();
            }
            File f = new File(d, fileName);
            if (!f.exists()) {
                f.createNewFile();
            }
            OutputStream os = new FileOutputStream(f);
            p.store(os, comment);
            os.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}