!java读取两个txt文件,并分析统计里面的数据后输出
求助!java读取两个txt文件,并分析统计里面的数据后输出
新手上路,接到需求,用java读取两个txt文件,并分析里面的数据后输出到新文件,详细情况如下:
A.txt:
编号| 日期| 编码| 操作| 账号
01 |20140221|12881|OPT |100000001288100001
02 |20140221|12885|OPT |100000001288500001
03 |20140221|12887|OPT |100000001288700001
B.txt(其中金额正数代表入金,负数代表出,结果为1代表成功,其他代表失败)
编号 |账号| 金额|结果
01|100000001288100001|88574|1
01|100000001288300001|5443|2
01|100000001288100001|45454|5
01|100000001288100001|8766|1
01|100000001288100001|-455|1
01|100000001288100001|-3445|1
要求统计同一账号的入金次数、出金次数,成功次数,输出格式要求如下:
编码 | 账号 | 出金 | 入金 | 成功次数
12881|100000001288100001|2|2|4
真心搞不定,求高手解答啊。。。。
------解决方案--------------------
首先,你结果里面的那个编码是什么意思?
我感觉要输出你的结果,只要b文件就可以了
给你个思路,扫描b文件,
然后建一个hashmap(K,V) key=帐号 value=new Integer[3]{入金次数,出金次数,成功次数}
然后扫描每一行做一个
while(hashmap.contains(K))
{
if(金额>0)入金次数+1
if(金额<0)出金次数+1
if(结果=1)成功次数+1
}
------解决方案--------------------
新手上路,接到需求,用java读取两个txt文件,并分析里面的数据后输出到新文件,详细情况如下:
A.txt:
编号| 日期| 编码| 操作| 账号
01 |20140221|12881|OPT |100000001288100001
02 |20140221|12885|OPT |100000001288500001
03 |20140221|12887|OPT |100000001288700001
B.txt(其中金额正数代表入金,负数代表出,结果为1代表成功,其他代表失败)
编号 |账号| 金额|结果
01|100000001288100001|88574|1
01|100000001288300001|5443|2
01|100000001288100001|45454|5
01|100000001288100001|8766|1
01|100000001288100001|-455|1
01|100000001288100001|-3445|1
要求统计同一账号的入金次数、出金次数,成功次数,输出格式要求如下:
编码 | 账号 | 出金 | 入金 | 成功次数
12881|100000001288100001|2|2|4
真心搞不定,求高手解答啊。。。。
------解决方案--------------------
首先,你结果里面的那个编码是什么意思?
我感觉要输出你的结果,只要b文件就可以了
给你个思路,扫描b文件,
然后建一个hashmap(K,V) key=帐号 value=new Integer[3]{入金次数,出金次数,成功次数}
然后扫描每一行做一个
while(hashmap.contains(K))
{
if(金额>0)入金次数+1
if(金额<0)出金次数+1
if(结果=1)成功次数+1
}
------解决方案--------------------
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;
public class FileCount {
public static void main(String[] args) throws Exception {
//先读取A.txt,将编码和账号存储到map中
FileReader afr = new FileReader("C:/java_test/A.txt");
BufferedReader ar = new BufferedReader(afr);
String aline = null;
Map<String, String> codeMap = new HashMap<String, String>();
while ((aline = ar.readLine()) != null) {
String[] array = aline.split("\\
------解决方案--------------------
");
codeMap.put(array[4], array[2]);
}
ar.close();
//再读取B.txt,将交易信息存储到map中
FileReader fr = new FileReader("C:/java_test/B.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
Map<String, int[]> map = new HashMap<String, int[]>();
while ((line = br.readLine()) != null) {
String[] array = line.split("\\
------解决方案--------------------
");
String acct = array[1];
int[] count = map.get(acct);
if (count == null) {
count = new int[] { 0, 0, 0 };
map.put(acct, count);
}
int amt = Integer.parseInt(array[2]);
if (amt >= 0) {
count[0] += 1;
} else {
count[1] += 1;
}
if ("1".equals(array[3])) {
count[2] += 1;
}
}
br.close();
FileWriter fw = new FileWriter("c:/java_test/C.txt");
BufferedWriter bw = new BufferedWriter(fw);
int j = 0;