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.Properties;

public class Main {
    public static boolean removeProperty(String filePath, String fileName, String propertyName) {
        try {
            Properties p = loadPropertyInstance(filePath, fileName);
            p.remove(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 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;
        }
    }
}