Properties 类:
测试代码1:
package test.com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Set;
//Properties 类是 Java 中一个非常实用的类,它继承自 Hashtable<Object,Object> 类,提供了更为方便的方法处理字符串键值对。
//特别适用于处理配置文件,因为它支持将属性列表加载到内存中,并且支持将内存中的属性列表写回到持久化存储中(如文件)。
//Properties 类中的键和值都是字符串类型的,使得它在处理文本数据时非常高效。
public class ConfigManager {
private Properties properties = new Properties();
// 设置属性
public void setProperty(String key, String value) {
properties.setProperty(key, value);
}
// 获取属性
public String getProperty(String key) {
return properties.getProperty(key);
}
// 加载属性文件
public void loadProperties(String filePath) throws IOException {
try (InputStream input = new FileInputStream(filePath)) {
properties.load(input);
}
}
// 保存属性到文件
public void saveProperties(String filePath, String comments) throws IOException {
try (OutputStream output = new FileOutputStream(filePath)) {
properties.store(output, comments);
}
}
// 打印所有属性
public void printAllProperties() {
Set<String> keys = properties.stringPropertyNames();
for (String key : keys) {
System.out.println(key + ": " + properties.getProperty(key));
}
}
// 测试
public static void main(String[] args) {
ConfigManager configManager = new ConfigManager();
// 设置属性
configManager.setProperty("username", "admin");
configManager.setProperty("password", "123456");
// 打印所有属性
configManager.printAllProperties();
// 假设有一个配置文件config.properties
try {
// 加载属性文件
configManager.loadProperties("config.properties");
// 再次打印所有属性,包括从文件加载的
configManager.printAllProperties();
// 保存属性到另一个文件
configManager.saveProperties("new_config.properties", "This is a new configuration file");
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果如下:
测试代码2:
创建一个 PropertiesManager类:
package test.com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
public class PropertiesManager {
private Properties properties;
public PropertiesManager() {
this.properties = new Properties();
}
/**
* 从指定的输入流加载属性
*
* @param inputStream 包含属性数据的输入流
* @throws IOException 如果发生I/O错误
*/
public void load(InputStream inputStream) throws IOException {
properties.load(inputStream);
}
/**
* 从指定的文件路径加载属性
*
* @param filePath 包含属性数据的文件路径
* @throws IOException 如果文件不存在或读取时发生错误
*/
public void loadFromFile(String filePath) throws IOException {
try (FileInputStream fis = new FileInputStream(filePath)) {
load(fis);
}
}
/**
* 将属性存储到指定的输出流
*
* @param outputStream 要写入属性数据的输出流
* @param comments 存储在文件开头的注释
* @throws IOException 如果发生I/O错误
*/
public void store(OutputStream outputStream, String comments) throws IOException {
properties.store(outputStream, comments);
}
/**
* 将属性存储到指定的文件路径
*
* @param filePath 要写入属性数据的文件路径
* @param comments 存储在文件开头的注释
* @throws IOException 如果文件无法写入或发生其他I/O错误
*/
public void storeToFile(String filePath, String comments) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
store(fos, comments);
}
}
/**
* 获取一个属性的值
*
* @param key 要获取的属性键
* @return 属性的值,如果键不存在则返回null
*/
public String getProperty(String key) {
return properties.getProperty(key);
}
// 可以添加更多方法处理Properties对象,如设置属性、列出所有属性等
}
测试类:
package test.com;
import java.io.*;
public class PropertiesManagerTest {
public static void main(String[] args) {
PropertiesManager configManager = new PropertiesManager();
try {
// 加载属性
configManager.loadFromFile("config.properties");
// 获取并打印属性
System.out.println(configManager.getProperty("database.url"));
// 存储属性到文件
configManager.storeToFile("new_config.properties", "Updated Configuration");
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果如下:
测试代码3:
一个简单的登录测试:
package test.com;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class LoginTestFromConfig {
public static void main(String[] args) {
// 配置文件路径
String configFilePath = "config.properties";
// 加载配置文件
Properties prop = new Properties();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(configFilePath);
prop.load(inputStream);
} catch (IOException ex) {
ex.printStackTrace();
return;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 从配置文件中读取用户名和密码
String correctUsername = prop.getProperty("username");
String correctPassword = prop.getProperty("password");
Scanner scanner = new Scanner(System.in);
int attempts = 0;
while (attempts < 3) {
System.out.print("请输入用户名: ");
String username = scanner.nextLine();
System.out.print("请输入密码: ");
String password = scanner.nextLine();
if (username.equals(correctUsername) && password.equals(correctPassword)) {
System.out.println("登录成功!");
break; // 成功登录后退出循环
} else {
System.out.println("用户名或密码错误,请重试。");
attempts++; // 登录失败,增加尝试次数
if (attempts == 3) {
System.out.println("用户名或密码错误,请明天再试。");
}
}
}
scanner.close();
}
}
运行结果如下: