IO流文件输出并处理
1.具体的功能:
文件中的内容:
通过Io流将文入数,并对内容进行如下处理:取所有记录、按客户号查询记录、按日期段查询记录、取得总金额、按金额排序。
2.实现的步骤:
首先我们要建立一个交易类(TransRecord),里面属性包含:客户号、 姓名、 所述机构号、 性别 (1:男;0:女)、帐号 、发生时间 、发生额;进行Set和Get处理,在声明一个包含所有属性的构造器,重写toString方法,因为要进行排序,所以我们在这个类里进行HashCode和equals方法的重写。
接着就要建立数据处理类(TransRecordManager),在类中声明一个带有泛型<TransRecord>的list集合,首先写加载数据的方法,将获取的字节流转换为字符流并包装成缓冲流,再将将一行文本解析为一个交易记录对象并添加到list集合;这样我们之后就只需要对list集合进行方法的编写实现功能。
最后我们要建立测试类(Test)对数据处理类(TransRecordManager)中的方法进行测试就行了。
3.代码编辑上述内容:
交易类(TransRecord)
public class TransRecord {
/**客户号*/
private String num;
/**姓名*/
private String name;
/**机构号*/
private String jnum;
/**性别*/
private String sex;
/**账号*/
private String account;
/**发生时间*/
private Date time;
/**发生金额*/
private BigDecimal cash;
public TransRecord() {
}
public TransRecord(String num, String name, String jnum, String sex, String account, Date time, BigDecimal cash) {
super();
this.num = num;
this.name = name;
this.jnum = jnum;
this.sex = sex;
this.account = account;
this.time = time;
this.cash = cash;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJnum() {
return jnum;
}
public void setJnum(String jnum) {
this.jnum = jnum;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public BigDecimal getCash() {
return cash;
}
public void setCash(BigDecimal cash) {
this.cash = cash;
}
@Override
public String toString() {
return "TransRecord [num=" + num + ", name=" + name + ", jnum=" + jnum + ", sex=" + sex + ", account=" + account
+ ", time=" + time + ", cash=" + cash + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((account == null) ? 0 : account.hashCode());
result = prime * result + ((cash == null) ? 0 : cash.hashCode());
result = prime * result + ((jnum == null) ? 0 : jnum.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
result = prime * result + ((time == null) ? 0 : time.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TransRecord other = (TransRecord) obj;
if (account == null) {
if (other.account != null)
return false;
} else if (!account.equals(other.account))
return false;
if (cash == null) {
if (other.cash != null)
return false;
} else if (!cash.equals(other.cash))
return false;
if (jnum == null) {
if (other.jnum != null)
return false;
} else if (!jnum.equals(other.jnum))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
if (time == null) {
if (other.time != null)
return false;
} else if (!time.equals(other.time))
return false;
return true;
}
}
数据处理类(TransRecordManager)
public class TransRecordManager {
// 声明一个成员变量,用于存储交易记录数据
private List<TransRecord> records = new ArrayList<>();
/**
* 加载数据
* @param in - 数据流
* @return
* @throws - 解析过程中IO错误
*/
public void load(InputStream in) throws IOException {
//将获取的字节流转换为字符流并包装成缓冲流
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line=br.readLine())!=null){
//排除注释行(以“#”开头)
if(!line.startsWith("#")){
TransRecord record = parseDataToObj(line);
records.add(record);
}
}
}
/**
* 将一行文本解析为一个交易记录对象
* @param line
* @return
*/
private TransRecord parseDataToObj(String line) {
TransRecord tr = new TransRecord();
String[] data = line.split("\\|");
tr.setNum(data[0]);
tr.setName(data[1]);
tr.setJnum(data[2]);
tr.setSex(Objects.equals(data[3], "1") ? "男" : "女");
tr.setAccount(data[4]);
tr.setCash(new BigDecimal(data[6]));
try {
DateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
Date time = fmt.parse(data[5]);
tr.setTime(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tr;
}
/**
* 加载数据
* @param fileName - 包含记录数据的文件名
* @return
* @throws - 解析过程中IO错误
*/
public void load(String fileName) throws IOException {
File f = new File(fileName);
InputStream is = new FileInputStream(f);
load(is);
}
/**
* 取所有记录
*
* @return 所有记录数组或null
*/
public List<TransRecord> getAll() {
return records;
}
/**
* 按客户号查询记录
*
* @param customerNumber
* - 客户号
* @return 符合条件的记录数组或null
*/
public List<TransRecord> findByCustomerNumber(String customerNumber) {
return records.stream()
.filter(r->Objects.equals(r.getNum(), customerNumber))
.collect(Collectors.toList());
}
/**
* 按日期段查询记录
* @param start - 开始日期
* @param end - 结束日期
* @return 符合条件的记录数组或null
*/
public List<TransRecord> findByDate(String start, String end) {
try {
DateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
long from = fmt.parse(start).getTime();
long to = fmt.parse(end).getTime();
// List<TransRecord> list = new ArrayList<>();
// for(TransRecord tr : records){
// long now = tr.getTime().getTime();
// if(now >= from && now <=to){
// list.add(tr);
// }
// }
// return list;
return records.stream()
.filter(r->{
long now = r.getTime().getTime();
return now>=from && now<=to;
})
.collect(Collectors.toList());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 取得总金额
*
* @return 总金额
*/
public BigDecimal totalAmount() {
BigDecimal result = new BigDecimal(0);
for(TransRecord tr : records){
BigDecimal cash = tr.getCash();
result = result.add(cash);
}
return result;
}
/**
* 按金额排序
* @return 按金额升序排序的结果
*/
public List<TransRecord> sortByAmount() {
Collections.sort(records,(r1,r2)->r1.getCash().compareTo(r2.getCash()));
return records;
}
}
测试类(Test)
public class Test {
public static void main(String[] args) throws IOException {
TransRecordManager trm=new TransRecordManager();
trm.load("src/record.txt");
//查询所有
for(TransRecord tr : trm.getAll()){
System.out.println(tr);
}
//根据客户号查询
List<TransRecord> list = trm.findByCustomerNumber("000001");
for(TransRecord tr : list){
System.out.println(tr);
}
//查询指定日期之间的记录
// list = trm.findByDate("20060720000000", "20060722000000");
//获取交易总金额
// System.out.println(trm.totalAmount());
//根据交易金额升序排序
list = trm.sortByAmount();
for (TransRecord tr : list) {
System.out.println(tr);
}
}
}