?? fileaccesser.java
字號:
package banking;
import java.util.*;
import java.io.*;
/**
* 對保存帳戶信息的數據文件進行讀取操作
* 文件格式:與屬性文件格式相同("key=value"的形式)
* @author rainliu
*/
public class FileAccesser {
/** 保存帳號信息的數據文件 */
private static String dataFileName = "src/banking/account.info";
/** 文件中數據信息的分隔符 */
private static final String DELIM = "=";
/** 表示該行注釋的符號 */
private static final String COMMENT = "#";
public FileAccesser() {
}
/**
* 從數據文件中讀取帳戶信息
* @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 {
RandomAccessFile raf = new RandomAccessFile(dataFileName, "rw");
int index;
String s;
while((s=raf.readLine())!=null) {
s = s.trim();
//忽視注釋信息
if (s.startsWith(COMMENT)) continue;
//若行中間有注釋,也去掉
if ((index = s.indexOf(COMMENT))!=-1) {
s = s.substring(0,index).trim();
}
//分離出參數名和參數值
if ((index = s.indexOf(DELIM))!=-1) {
String key = s.substring(0,index).trim();
String value = s.substring(index+1).trim();
map.put(key,value);
}
}
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 將帳戶信息列表寫入數據文件中
* @param map 要保存到文件中的帳戶信息
* @return boolean 操作成功返回true,否則返回false
*/
private boolean writeData(Map map) {
boolean result = false;
Vector vec=new Vector();
try {
//先讀出文件的所有信息,并根據要保存的帳戶信息進行相應更新
RandomAccessFile raf = new RandomAccessFile(dataFileName, "r");
int index;
String s;
while((s=raf.readLine())!=null) {
s = s.trim();
//分離出參數名和參數值
if (!s.startsWith(COMMENT) && (index = s.indexOf(DELIM))!=-1) {
String key = s.substring(0,index).trim();
String value = s.substring(index+1).trim();
//修改參數值
if (map.containsKey(key)) {
s = key + DELIM + map.get(key);
}
}
vec.addElement(s);
}
raf.close();
//將最新的信息保存到文件中
java.io.FileWriter fw = new java.io.FileWriter(dataFileName);
BufferedWriter writer = new BufferedWriter(fw);
//清除原有文件中的信息
writer.write("");
String ls = System.getProperty("line.separator");
for (int intCount = 0; intCount < vec.size(); intCount++) {
//讀取信息
String strWriteRecord = (String)vec.elementAt(intCount);
//編碼格式轉換
strWriteRecord = new String(strWriteRecord.getBytes("ISO-8859-1"), "GB2312");
//信息寫入
writer.write(strWriteRecord.trim() + ls);
}
writer.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 + -