propertiesutil.java#1.1

来自「数据库远程同步软件NetBeans项目源文件 项目采用Jdesktop集成组件」· 1 代码 · 共 134 行

1
134
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package databasesyncdesktopapplication;import com.qixuan.jdbc.util.CalendarUtil;import databasesyncdesktopapplication.entity.Propertie;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import java.util.Properties;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author mjw917 */public class PropertiesUtil {    static{        init();    }     // "C:/Documents and Settings/LocalService/Application Data/qixuan/PropertiesUtil.properties";    //"D:/NetBeans_work/DataBaseSyncDesktopApplication/src/conf/PropertiesUtil.properties";private static final String filePath = "D:/NetBeans_work/DataBaseSyncDesktopApplication/src/conf/PropertiesUtil.properties";private static Properties info ;private static Long count;private Propertie propertie;    public Propertie getPropertie() {        return propertie;    }    public static long getCount() {        return count.longValue();    }    public void setPropertie(Propertie propertie) {        this.propertie = propertie;    }private static void init(){    info = new Properties();   InputStream in = null; try {			//获取输入流			in = new FileInputStream(filePath);			info.load(in);		} catch (IOException e) {            in = Thread					.currentThread()					.getContextClassLoader()					.getResourceAsStream(filePath);            try {                System.out.println(in==null);                info.load(in);                //e.printStackTrace();            } catch (IOException ex) {                e.printStackTrace();                Logger.getLogger(PropertiesUtil.class.getName()).log(Level.SEVERE, null, ex);            }			//e.printStackTrace();		} finally {			if(in != null)				try {					in.close();				} catch (IOException e) {					e.printStackTrace();				}		}               if(info.getProperty("count")==null){info.setProperty("count", String.valueOf(0));File file=new File(filePath);            FileOutputStream fout;            try {                fout = new FileOutputStream(file);                try {                    info.store(fout, filePath);                } catch (IOException ex) {                    Logger.getLogger(PropertiesUtil.class.getName()).log(Level.SEVERE, null, ex);                }            } catch (FileNotFoundException ex) {                Logger.getLogger(PropertiesUtil.class.getName()).log(Level.SEVERE, null, ex);            }                  }else{            count=Long.valueOf(info.getProperty("count"));        }}    public static void save(Propertie propertie){        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd   HH:mm:ss");		bartDateFormat.setLenient(false);        count++;        try {            info.setProperty("planId[" + count+"]", propertie.getId().toString());            info.setProperty("name[" + count+"]", propertie.getName());            info.setProperty("sourceFilePath[" + count+"]", propertie.getSourceFilePath());            info.setProperty("targetFilePath[" + count+"]", propertie.getTargetFilePath());            info.setProperty("taskTime[" + count+"]",  bartDateFormat.format(propertie.getTaskTime()));            info.setProperty("count",count.toString());            File file=new File(filePath);            FileOutputStream fout=new FileOutputStream(file);            info.store(fout, filePath);        } catch (IOException ex) {            Logger.getLogger(PropertiesUtil.class.getName()).log(Level.SEVERE, null, ex);        }    }    /*     *读取配置文件中的键值信息     */ public static List getProperties(){     List<Propertie> list=new ArrayList<Propertie>(); for(int i=1;i<=count;i++){     Propertie propertie=new Propertie();     propertie.setId(Long.valueOf(info.getProperty("planId[" + i+"]")));     propertie.setName(info.getProperty("name[" + i+"]"));     propertie.setSourceFilePath(info.getProperty("sourceFilePath[" + i+"]"));     propertie.setTargetFilePath(info.getProperty("targetFilePath[" + i+"]"));     propertie.setTaskTime(CalendarUtil.getNowdate(info.getProperty("taskTime[" + i+"]")));     list.add(propertie); }     return list; }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?