java 读取,写入 txt 文件示例
2016-04-13 14:30
525 查看
一:示例背景介绍:
本示例为了处理里相同格式的A,B两个txt 文件,筛选出文件中的不同即有三种情况:
1.A 中有的,B中没有的 ;
2.B中有的,A中没有的;
3.A,B两者都有的,不过值不完全相同;
二:具体实现:
文本格式形如:
java 代码如下:
java bean :
import java.math.BigDecimal;
public class FundBean {
// 商户标识
private String custId;
// 交易账号
private String tradeAccount;
// 基金代码
private String fundCode;
// 总份额
private BigDecimal countFen;
// 冻结份额
private BigDecimal lockFen;
// 可用份额
private BigDecimal useFen;
// 未分配收益
private BigDecimal notFee;
// 分红方式 1: 现金分红 0:红利再投
private String flag;
public String getCustId() {
return custId;
}
public void setCustId(String custId) {
this.custId = custId;
}
public String getTradeAccount() {
return tradeAccount;
}
public void setTradeAccount(String tradeAccount) {
this.tradeAccount = tradeAccount;
}
public String getFundCode() {
return fundCode;
}
public void setFundCode(String fundCode) {
this.fundCode = fundCode;
}
public BigDecimal getCountFen() {
return countFen;
}
public void setCountFen(BigDecimal countFen) {
this.countFen = countFen;
}
public BigDecimal getLockFen() {
return lockFen;
}
public void setLockFen(BigDecimal lockFen) {
this.lockFen = lockFen;
}
public BigDecimal getUseFen() {
return useFen;
}
public void setUseFen(BigDecimal useFen) {
this.useFen = useFen;
}
public BigDecimal getNotFee() {
return notFee;
}
public void setNotFee(BigDecimal notFee) {
this.notFee = notFee;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof FundBean) {
FundBean et = (FundBean) obj;
// 若BigDecimal 中存放 1.0 与 1.00 则不能用 equals,二者比较不等,但是实际上数学上是相等的
if ((et.countFen.equals(this.countFen))
&& (et.lockFen.equals(this.lockFen))
&& (et.useFen.equals(this.useFen))
&& (et.notFee.equals(this.notFee))
&& (et.custId == this.custId || et.custId
.equals(this.custId))
&& (et.tradeAccount == this.tradeAccount || et.tradeAccount
.equals(this.tradeAccount))
&& (et.fundCode == this.fundCode || et.fundCode
.equals(this.fundCode))
&& (et.flag == this.flag || et.flag.equals(this.flag))) {
return true;
}
}
return false;
}
public String toString(){
String str = this.custId +"|" + this.tradeAccount + "|" + this.fundCode + "|"
+ this.countFen.toString() + "|" + this.lockFen.toString() + "|" + this.useFen.toString() + "|"
+ this.notFee.toString() +"|" + this.flag;
return str ;
}
}
java 方法:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TextRead {
public static void readTxtFile(List<FundBean> list,Map<String,FundBean> map,String filePath){
// List<FundBean> list = new ArrayList<FundBean>();
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String text = "";
while((text = bufferedReader.readLine())!= null){
System.out.println(text);
if(!("".equals(text))){
FundBean obj = new FundBean();
String[] ss = text.split("\\|");
obj.setCustId(ss[0]);
obj.setTradeAccount(ss[1]);
obj.setFundCode(ss[2]);
//obj.setCountFen(Double.parseDouble(ss[3]));
obj.setCountFen(new BigDecimal(ss[3]));
obj.setLockFen(new BigDecimal(ss[4]));
obj.setUseFen(new BigDecimal(ss[5]));
obj.setNotFee(new BigDecimal(ss[6]));
obj.setFlag(ss[7]);
list.add(obj);
map.put(obj.getCustId(), obj);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
//存放360 有,众禄没有的
static List<FundBean> list1 = new ArrayList<FundBean>();
//存放众禄有的,360 没有有
static List<FundBean> list2 = new ArrayList<FundBean>();
//两者都有,不过值不一样的
static List<FundBean> list3 = new ArrayList<FundBean>();
public static void main(String[] args) {
String filePath1 = "C:\\Users\\joey\\Desktop\\test360.txt";
String filePath2 = "C:\\Users\\joey\\Desktop\\test.txt";
List<FundBean> list360 = new ArrayList<FundBean>();
List<FundBean> listZL = new ArrayList<FundBean>();
Map<String,FundBean> map360 = new HashMap<String,FundBean>();
Map<String,FundBean> mapzl = new HashMap<String,FundBean>();
readTxtFile(list360,map360,filePath1);
readTxtFile(listZL,mapzl,filePath2);
System.out.println("fffffffff");
//360有的,众禄没有的
for (int i = 0; i < list360.size(); i++) {
FundBean fub = list360.get(i);
String custId = fub.getCustId();
FundBean zlobject = mapzl.get(custId);
if(zlobject==null){
list1.add(fub);
}else{
//两者都有,并且值不一样的
if(!fub.equals(zlobject)){
list3.add(zlobject);
}
}
}
//众禄有的,360没有的
for (int i = 0; i < listZL.size(); i++) {
FundBean fub = listZL.get(i);
String custId = fub.getCustId();
FundBean obj = map360.get(custId);
if(obj==null){
list2.add(fub);
}
}
//输出到文件
//360有的记录,众禄没有的
String fileName360 = "C:\\Users\\joey\\Desktop\\compareFile\\360.txt";//list1
//众禄有的,360没有的
String fileNameZL = "C:\\Users\\joey\\Desktop\\compareFile\\ZL.txt";//list2
//两者都有,不过值不一样
String fileNameDif = "C:\\Users\\joey\\Desktop\\compareFile\\dif.txt";//list3
StringBuffer stb360 = new StringBuffer();
for (int i = 0; i < list1.size(); i++) {
FundBean be = list1.get(i);
String str = be.toString();
stb360.append(str+"\n");
}
//写入
writeFile(fileName360,stb360.toString());
StringBuffer stbZL = new StringBuffer();
for (int i = 0; i < list2.size(); i++) {
FundBean be = list2.get(i);
String str = be.toString();
stbZL.append(str+"\n");
}
//写入
writeFile(fileNameZL,stbZL.toString());
StringBuffer stbDIF = new StringBuffer();
for (int i = 0; i < list3.size(); i++) {
FundBean be = list3.get(i);
String str = be.toString();
stbDIF.append(str+"\n");
}
//写入
writeFile(fileNameDif,stbDIF.toString());
System.exit(0);
}
/**
* 写入到磁盘中
* @param fileName
* @param fileContent
*/
public static void writeFile(String fileName, String fileContent)
{
try
{
File f = new File(fileName);
if (!f.exists())
{
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"gbk");
BufferedWriter writer=new BufferedWriter(write);
writer.write(fileContent);
writer.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
本示例为了处理里相同格式的A,B两个txt 文件,筛选出文件中的不同即有三种情况:
1.A 中有的,B中没有的 ;
2.B中有的,A中没有的;
3.A,B两者都有的,不过值不完全相同;
二:具体实现:
文本格式形如:
java 代码如下:
java bean :
import java.math.BigDecimal;
public class FundBean {
// 商户标识
private String custId;
// 交易账号
private String tradeAccount;
// 基金代码
private String fundCode;
// 总份额
private BigDecimal countFen;
// 冻结份额
private BigDecimal lockFen;
// 可用份额
private BigDecimal useFen;
// 未分配收益
private BigDecimal notFee;
// 分红方式 1: 现金分红 0:红利再投
private String flag;
public String getCustId() {
return custId;
}
public void setCustId(String custId) {
this.custId = custId;
}
public String getTradeAccount() {
return tradeAccount;
}
public void setTradeAccount(String tradeAccount) {
this.tradeAccount = tradeAccount;
}
public String getFundCode() {
return fundCode;
}
public void setFundCode(String fundCode) {
this.fundCode = fundCode;
}
public BigDecimal getCountFen() {
return countFen;
}
public void setCountFen(BigDecimal countFen) {
this.countFen = countFen;
}
public BigDecimal getLockFen() {
return lockFen;
}
public void setLockFen(BigDecimal lockFen) {
this.lockFen = lockFen;
}
public BigDecimal getUseFen() {
return useFen;
}
public void setUseFen(BigDecimal useFen) {
this.useFen = useFen;
}
public BigDecimal getNotFee() {
return notFee;
}
public void setNotFee(BigDecimal notFee) {
this.notFee = notFee;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof FundBean) {
FundBean et = (FundBean) obj;
// 若BigDecimal 中存放 1.0 与 1.00 则不能用 equals,二者比较不等,但是实际上数学上是相等的
if ((et.countFen.equals(this.countFen))
&& (et.lockFen.equals(this.lockFen))
&& (et.useFen.equals(this.useFen))
&& (et.notFee.equals(this.notFee))
&& (et.custId == this.custId || et.custId
.equals(this.custId))
&& (et.tradeAccount == this.tradeAccount || et.tradeAccount
.equals(this.tradeAccount))
&& (et.fundCode == this.fundCode || et.fundCode
.equals(this.fundCode))
&& (et.flag == this.flag || et.flag.equals(this.flag))) {
return true;
}
}
return false;
}
public String toString(){
String str = this.custId +"|" + this.tradeAccount + "|" + this.fundCode + "|"
+ this.countFen.toString() + "|" + this.lockFen.toString() + "|" + this.useFen.toString() + "|"
+ this.notFee.toString() +"|" + this.flag;
return str ;
}
}
java 方法:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TextRead {
public static void readTxtFile(List<FundBean> list,Map<String,FundBean> map,String filePath){
// List<FundBean> list = new ArrayList<FundBean>();
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String text = "";
while((text = bufferedReader.readLine())!= null){
System.out.println(text);
if(!("".equals(text))){
FundBean obj = new FundBean();
String[] ss = text.split("\\|");
obj.setCustId(ss[0]);
obj.setTradeAccount(ss[1]);
obj.setFundCode(ss[2]);
//obj.setCountFen(Double.parseDouble(ss[3]));
obj.setCountFen(new BigDecimal(ss[3]));
obj.setLockFen(new BigDecimal(ss[4]));
obj.setUseFen(new BigDecimal(ss[5]));
obj.setNotFee(new BigDecimal(ss[6]));
obj.setFlag(ss[7]);
list.add(obj);
map.put(obj.getCustId(), obj);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
//存放360 有,众禄没有的
static List<FundBean> list1 = new ArrayList<FundBean>();
//存放众禄有的,360 没有有
static List<FundBean> list2 = new ArrayList<FundBean>();
//两者都有,不过值不一样的
static List<FundBean> list3 = new ArrayList<FundBean>();
public static void main(String[] args) {
String filePath1 = "C:\\Users\\joey\\Desktop\\test360.txt";
String filePath2 = "C:\\Users\\joey\\Desktop\\test.txt";
List<FundBean> list360 = new ArrayList<FundBean>();
List<FundBean> listZL = new ArrayList<FundBean>();
Map<String,FundBean> map360 = new HashMap<String,FundBean>();
Map<String,FundBean> mapzl = new HashMap<String,FundBean>();
readTxtFile(list360,map360,filePath1);
readTxtFile(listZL,mapzl,filePath2);
System.out.println("fffffffff");
//360有的,众禄没有的
for (int i = 0; i < list360.size(); i++) {
FundBean fub = list360.get(i);
String custId = fub.getCustId();
FundBean zlobject = mapzl.get(custId);
if(zlobject==null){
list1.add(fub);
}else{
//两者都有,并且值不一样的
if(!fub.equals(zlobject)){
list3.add(zlobject);
}
}
}
//众禄有的,360没有的
for (int i = 0; i < listZL.size(); i++) {
FundBean fub = listZL.get(i);
String custId = fub.getCustId();
FundBean obj = map360.get(custId);
if(obj==null){
list2.add(fub);
}
}
//输出到文件
//360有的记录,众禄没有的
String fileName360 = "C:\\Users\\joey\\Desktop\\compareFile\\360.txt";//list1
//众禄有的,360没有的
String fileNameZL = "C:\\Users\\joey\\Desktop\\compareFile\\ZL.txt";//list2
//两者都有,不过值不一样
String fileNameDif = "C:\\Users\\joey\\Desktop\\compareFile\\dif.txt";//list3
StringBuffer stb360 = new StringBuffer();
for (int i = 0; i < list1.size(); i++) {
FundBean be = list1.get(i);
String str = be.toString();
stb360.append(str+"\n");
}
//写入
writeFile(fileName360,stb360.toString());
StringBuffer stbZL = new StringBuffer();
for (int i = 0; i < list2.size(); i++) {
FundBean be = list2.get(i);
String str = be.toString();
stbZL.append(str+"\n");
}
//写入
writeFile(fileNameZL,stbZL.toString());
StringBuffer stbDIF = new StringBuffer();
for (int i = 0; i < list3.size(); i++) {
FundBean be = list3.get(i);
String str = be.toString();
stbDIF.append(str+"\n");
}
//写入
writeFile(fileNameDif,stbDIF.toString());
System.exit(0);
}
/**
* 写入到磁盘中
* @param fileName
* @param fileContent
*/
public static void writeFile(String fileName, String fileContent)
{
try
{
File f = new File(fileName);
if (!f.exists())
{
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"gbk");
BufferedWriter writer=new BufferedWriter(write);
writer.write(fileContent);
writer.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树