📄 fileproperties.java
字号:
package banking;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
/**
* 对保存帐户信息的数据文件进行读取操作
* 利用Properties类的有关方法
* 文件格式:与属性文件格式相同("key=value"的形式)
* @author rainliu
*/
public class FileProperties {
/** 保存帐号信息的数据文件 */
private static String dataFileName = "src/banking/account.properties";
public FileProperties() {
}
/**
* 从数据文件中读取帐户信息
* @return List 帐户信息的参数名和参数值
*/
public List readAccountInfo() {
//检查数据文件是否存在
if (!checkFile(dataFileName)) return null;
//保存一组用户信息的列表
List accountList = new ArrayList();
//从文件中读取数据
Map map = readData();
//若文件中没有任何数据,返回空的信息列表
if (map==null || map.size()<1) return accountList;
//将读出的数据转化为帐户信息列表
String allId = (String)map.get("accountId");
String allPass = (String)map.get("password");
String allBalance = (String)map.get("balance");
//分出各个帐户的信息
StringTokenizer stId = new StringTokenizer(allId,",");
StringTokenizer stPass = new StringTokenizer(allPass,",");
StringTokenizer stBalance = new StringTokenizer(allBalance,",");
while (stId.hasMoreTokens()) {
String tempID = stId.nextToken();
String tempPass = stPass.nextToken();
String strBalance = stBalance.nextToken();
double tempBalance = Double.parseDouble(strBalance);
AccountInfo ai = new AccountInfo(tempID,tempPass,tempBalance);
accountList.add(ai);
}
return accountList;
}
/**
* 将帐户信息列表写入数据文件中
* @param List 要保存到文件中的帐户信息
* @return boolean 操作成功返回true,否则返回false
*/
public boolean writeAccountInfo(List accountList) {
boolean result = false;
//检查数据文件是否存在
if (!checkFile(dataFileName)) return false;
//将帐户信息列表转换成key和value映射的格式
//所有帐户的同类信息以“,”连接
String allId = "";
String allPass = "";
String allBalance = "";
for (int i=0;i<accountList.size();i++) {
AccountInfo tempAccount = (AccountInfo)accountList.get(i);
allId += tempAccount.accountId + ",";
allPass += tempAccount.password + ",";
allBalance += tempAccount.balance + ",";
}
//去掉末尾的逗号
if (allId.endsWith(",")) {
allId = allId.substring(0,allId.length()-1);
allPass = allPass.substring(0,allPass.length()-1);
allBalance = allBalance.substring(0,allBalance.length()-1);
}
Map map = new HashMap();
map.put("accountId",allId);
map.put("password",allPass);
map.put("balance",allBalance);
//写入文件
if (writeData(map)==true) {
result = true;
}
return result;
}
/**
* 从数据文件中读取帐户信息
* @return Map 帐户信息的参数名和参数值
*/
private Map readData() {
//保存帐户信息的参数名和参数值
Map map = new HashMap();
try {
FileInputStream fis = new FileInputStream(dataFileName);
Properties ppt = new Properties();
ppt.load(fis);
//将属性列表的数据保存到map中
Enumeration er = ppt.propertyNames();
while (er.hasMoreElements()) {
String key = (String)er.nextElement();
if (key!=null && !"".equals(key.trim())) {
map.put(key,ppt.getProperty(key));
}
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 将帐户信息列表写入数据文件中
* @param map 要保存到文件中的帐户信息
* @return boolean 操作成功返回true,否则返回false
*/
private boolean writeData(Map map) {
boolean result = false;
try {
//将要保存的数据从map中转入ppt中
Properties ppt = new Properties();
//map中所有key的集合
Set set = map.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
String key = (String)iterator.next();
ppt.setProperty(key,(String)map.get(key));
}
//将ppt中的所有数据输出到文件
FileOutputStream fos = new FileOutputStream(dataFileName);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String header = dateFormat.format(new java.util.Date());
ppt.store(fos,header);
fos.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 检查文件是否存在,若不存在,则新建
* @return boolean 操作成功返回true,否则返回false
*/
public boolean checkFile(String fileName) {
boolean result = false;
try {
//如果该文件不存在,则在banking目录下创建一个新的文件
File file = new File(fileName);
if (!file.exists()) {
//若路径不存在,先创建路径
//File pathFile = dataFile.getParentFile();
File pathFile = new File(file.getParent());
if (!pathFile.exists()) {
pathFile.mkdirs();
}
//在指定路径下创建该文件
file.createNewFile();
}
result = true;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -