50,530
社区成员
发帖
与我相关
我的任务
分享
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ProcessBill{
public static void main(String[] args){
String content = loadContent("file.bin");
HashMap<String,Bill> billRecords = process(content);
Set<String> keySet = billRecords.keySet();
Iterator<String> keys = keySet.iterator();
while(keys.hasNext()){
String key = keys.next();
Bill bill = billRecords.get(key);
System.out.printf("%s:%s\n",key,bill);
}
}
private static String loadContent(String filePath){
StringBuilder result = new StringBuilder();
Path path = Paths.get(filePath);
try(Scanner file = new Scanner(path);){
while(file.hasNextLine()){
result.append(file.nextLine());
}
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
return result.toString();
}
private static HashMap<String,Bill> process(String content){
HashMap<String,Bill> result = new HashMap<>();
String regex = "\"(?<key>[^\"]+)\":\\[\"(?<billType>[^\"]+)\"\\s*,\\s*\"(?<billDate>[^\"]+)\"\\s*,\\s*\"(?<submitPerson>[^\"]+)\"\\s*,\\s*\"(?<billNumber>[^\"]+)\"\\s*,\\s*\"(?<accountNumber>[^\"]+)\"\\s*,\\s*\"(?<accountName>[^\"]+)\"\\s*,\\s*\"(?<sheetName>[^\"]+)\"\\]";
Matcher matcher = Pattern.compile(regex).matcher(content);
String billType,billDate,submitPerson,billNumber,accountNumber,accountName,sheetName;
String key;
while(matcher.find()){
key = matcher.group("key");
billType = matcher.group("billType");
billDate = matcher.group("billDate");
submitPerson = matcher.group("submitPerson");
billNumber = matcher.group("billNumber");
accountNumber = matcher.group("accountNumber");
accountName = matcher.group("accountName");
sheetName = matcher.group("sheetName");
result.put(key,new Bill(billType,billDate,submitPerson,billNumber,accountNumber,accountName,sheetName));
}
return result;
}
private static class Bill{
public Bill(String billType,String billDate,String submitPerson,String billNumber,String accountNumber,String accountName,String sheetName){
this.billType = billType;
this.billDate = billDate;
this.submitPerson = submitPerson;
this.billNumber = billNumber;
this.accountNumber = accountNumber;
this.accountName = accountName;
this.sheetName = sheetName;
}
@Override
public String toString(){
return String.format("[%s,%s,%s,%s,%s,%s,%s]",billType,billDate,submitPerson,billNumber,accountNumber,accountName,sheetName);
}
private String billType;
private String billDate;
private String submitPerson;
private String billNumber;
private String accountNumber;
private String accountName;
private String sheetName;
}
}