java 读取写入_java 读取,写入 txt 文件示例

一:示例背景介绍:

本示例为了处理里相同格式的A,B两个txt 文件,筛选出文件中的不同即有三种情况:

1.A 中有的,B中没有的 ;

2.B中有的,A中没有的;

3.A,B两者都有的,不过值不完全相同;

二:具体实现:

文本格式形如:

0818b9ca8b590ca3270a3433284dd417.png

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 list,Map map,String filePath){   //  List list = new ArrayList();           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 list1 = new ArrayList(); //存放众禄有的,360 没有有      static List list2 = new ArrayList();      //两者都有,不过值不一样的      static List list3 = new ArrayList();   public static void main(String[] args) { String filePath1 = "C:\\Users\\joey\\Desktop\\test360.txt"; String filePath2 = "C:\\Users\\joey\\Desktop\\test.txt"; List list360 = new ArrayList(); List listZL = new ArrayList(); Map map360 = new HashMap(); Map mapzl = new HashMap(); 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();         }   }   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值