java中.properties属性文件的使用案例源码

一、描述

java中的.properties属性文件的正确使用可以解决很多问题,比如一个登录界面要做一个记住用户登录过的用户名和密码并且放在本地方便用户登录。

二、操作步骤

1.  打开eclipse工程文件目录下的XX.properties文件,如果没有就创建一个

2. 以键-值对的方式记录用户最近登录过的用户名--密码,添加一个键值对

3. 移除一个键-值对

4. 保存这个属性文件

5. 获取属性文件的所有键

6. 获取指定键的属性值

二、源代码

//在工作主目录下(即eclipse项目目录下创建一个属性文件,例如用来记录用户最近登录过的用户名和密码)
File file = new File(System.getProperty("user.dir")	+ "\\login.properties");
if (!file.exists()) {
	//如果文件不存在就先创建
	file.createNewFile();
}
properties.load(new FileInputStream(file)); //加载属性文件

//获取属性文件中的某个属性,<span style="font-family: arial; line-height: 20px; background-color: rgb(255, 255, 255);">搜索此属性中指定键的属性值</span>
if (properties.getProperty("firstname") == null) {
	//设置属性文件中的某个属性
	properties.setProperty("firstname", "");
}

ArrayList<String> accountList = new ArrayList<String>();
@SuppressWarnings("unchecked")
//获取属性文件的所有key,放在一个String类型的枚举类型中
Enumeration<String> en = (Enumeration<String>) properties.propertyNames();
while (en.hasMoreElements()) {
	String key = (String) en.nextElement();
	accountList.add(key);
}
//将ArrayList<String>转换为字符串数组,将它放在一个下拉列表中供用户选择
String[] nameHistory = (String[]) accountList.toArray(new String[1]);
JComboBox accountBox = new JComboBox(nameHistory);

// 创建一个新的键值对,记录最后登录名为首选登录名,每次登录的默认选择项
properties.put("firstname", sname);

properties.remove(keyName); //删除一个keyName为键的记录

//保存属性文件,并捕获相应的异常
try {
	properties.store(new FileOutputStream(
			"login.properties"), null);
} catch (FileNotFoundException ex) {
	Logger.getLogger(LoginFrame.class.getName()).log(
			Level.SEVERE, null, ex);
} catch (IOException ex) {
	Logger.getLogger(LoginFrame.class.getName()).log(
			Level.SEVERE, null, ex);
}
	



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要手写一个Spring的源码实现是一个非常复杂的任务,需要对Spring的核心原理和设计思想有深入的理解。以下是一个简单的Spring源码实现的示例,仅供参考。 首先,我们需要创建一个核心的容器类来管理Bean的生命周期和依赖注入。这个容器类需要实现BeanFactory接口,提供getBean()方法来获取Bean对象。 ```java public interface BeanFactory { Object getBean(String name); } ``` 接下来,我们需要实现一个简单的Bean定义类,用于描述Bean的属性和依赖关系。 ```java public class BeanDefinition { private Class<?> beanClass; private Map<String, Object> properties = new HashMap<>(); private List<PropertyValue> propertyValues = new ArrayList<>(); public void setBeanClass(Class<?> beanClass) { this.beanClass = beanClass; } public Class<?> getBeanClass() { return beanClass; } public void addProperty(String name, Object value) { properties.put(name, value); } public Object getProperty(String name) { return properties.get(name); } public List<PropertyValue> getPropertyValues() { return propertyValues; } public void addPropertyValue(PropertyValue propertyValue) { propertyValues.add(propertyValue); } } public class PropertyValue { private final String name; private final Object value; public PropertyValue(String name, Object value) { this.name = name; this.value = value; } public String getName() { return name; } public Object getValue() { return value; } } ``` 然后,我们需要实现一个简单的BeanFactory实现类,用于创建和管理Bean对象。这个实现类需要读取Bean定义信息,创建Bean实例,并将其保存在一个Map。 ```java public class SimpleBeanFactory implements BeanFactory { private final Map<String, BeanDefinition> beanDefinitions = new HashMap<>(); private final Map<String, Object> beans = new HashMap<>(); public SimpleBeanFactory(String configLocation) { loadBeanDefinitions(configLocation); createBeans(); } private void loadBeanDefinitions(String configLocation) { // 从配置文件读取Bean定义信息 } private void createBeans() { for (String beanName : beanDefinitions.keySet()) { createBean(beanName); } } private Object createBean(String beanName) { BeanDefinition beanDefinition = beanDefinitions.get(beanName); Class<?> beanClass = beanDefinition.getBeanClass(); Object bean = createInstance(beanClass); applyPropertyValues(bean, beanDefinition.getPropertyValues()); beans.put(beanName, bean); return bean; } private Object createInstance(Class<?> beanClass) { try { return beanClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } private void applyPropertyValues(Object bean, List<PropertyValue> propertyValues) { for (PropertyValue propertyValue : propertyValues) { String propertyName = propertyValue.getName(); Object value = propertyValue.getValue(); setPropertyValue(bean, propertyName, value); } } private void setPropertyValue(Object bean, String propertyName, Object value) { try { BeanUtils.setProperty(bean, propertyName, value); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } @Override public Object getBean(String name) { return beans.get(name); } } ``` 最后,我们需要实现一个简单的测试类来验证我们的实现是否正确。 ```java public class SimpleBeanFactoryTest { @Test public void testGetBean() { BeanFactory beanFactory = new SimpleBeanFactory("classpath:beans.xml"); TestBean testBean = (TestBean) beanFactory.getBean("testBean"); assertNotNull(testBean); assertNotNull(testBean.getDependency()); } } ``` 这只是一个简单的Spring源码实现示例,实际上Spring的源码实现要复杂得多,涉及到很多高级特性和设计模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值