晚安,各位父老弟兄姊妹!
.......................遇到了难题,请各位豪杰伸出援助之手!
项目难点:从指定的文本获取特定字符项目Demo:
txt:
项目记录条数|总金额
abc|efg|hij|klmn|opqrst|u|v|www|xyy|zzzzzzzzzz|
abc2|efg2|hij2|klm2|opqrs2|u2|v2|www2|xyy2|zzzzzzzzzz2|
......
......
abc3|efg|hij|klmn|opqrst|u|v|www|xyy|zzzzzzzzzz4|
abc4|efg2|hij2|klm2|opqrs2|u2|v2|www2|xyy2|zzzzzzzzzz4|
我该如何循环读取(第二行以后)指定位置的信息字符比如(abc,klmn,u,zzzzz...)请问如何实现!
.......................遇到了难题,请各位豪杰伸出援助之手!
项目难点:从指定的文本获取特定字符项目Demo:
txt:
项目记录条数|总金额
abc|efg|hij|klmn|opqrst|u|v|www|xyy|zzzzzzzzzz|
abc2|efg2|hij2|klm2|opqrs2|u2|v2|www2|xyy2|zzzzzzzzzz2|
......
......
abc3|efg|hij|klmn|opqrst|u|v|www|xyy|zzzzzzzzzz4|
abc4|efg2|hij2|klm2|opqrs2|u2|v2|www2|xyy2|zzzzzzzzzz4|
我该如何循环读取(第二行以后)指定位置的信息字符比如(abc,klmn,u,zzzzz...)请问如何实现!
你的行与行之间是独立关系还是连续关系?!如果是独立关系,就用换行符来拆,string.split("\n"),这样可以定位到每一行,再取出每一行的内容,在用string.split("|"),拆成一个字符串的数组,就可以准确定位到你要的字符串了!
这样就可以得到
String path = System.getProperty("user.dir");
这个也好处理啊,就是把要生成的目标文件的路径写在属性文件里,需要的时候去加载就可以了!配一个config.properties 文件,把路径配在里面,
writPath d:\\
加载路径:
String writPath = null;
InputStream in = this.getClass().getResourceAsStream("/config.properties");
Properties p = new Properties();
p.load(in);synchronized (this) {
writPath = p.getProperty("writPath");
}
File file = new File(path);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while(br.readLine() != null)
{
temp = br.readLine();
String[] tem = temp.split("|");
……
}配置文件里设置一个属性,值自己设,然后程序取,和其他读取配置文件方法完全一样
好像有个System.getProperty()方法
GH_SB_ PLKK_20081113171253.txt:
2|2334.0
377|10362187|郭春梅|1|1|11|179|200806-200806|20|20080808|200|11|113467497
381|10364549|朱永贵|12221212|11111111|11|2155|200707-200806|20|20080808|200|11|113467497
............
(格式:
第一行:总笔数 总金额
第二行到文本结尾
文档号|社保号|个人姓名|协议号码|银行帐号|险种类型|征集金额|摘要|缴费类型|扣款时间|扣款金额|银行行号|银行流水号
)
我要读取第一行总笔数,总金额
顺序循环读取一下每一行的关键字段文档号,扣款金额,银行行号,扣款时间
项目代码:
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
//
System.out.println("filename3" + file.getAbsolutePath());
//读取扣款成功的文本
int id = 0;
double totalMoney = 0;
int totalRecord=0;
String sbh = "";
String yhbj = "";
int kksj = 0;
double kkje = 0;
String s = br.readLine();
String[] temp = s.split("\\|");
//读取首行记录
if (s != null) {
totalRecord = Integer.parseInt(temp[0].trim());
totalMoney = Double.parseDouble(temp[1].trim());
s = br.readLine();
}
while (s != null) {
id = Integer.parseInt(temp[0].trim());
sbh = data[1].trim();
kksj = Integer.parseInt(temp[9].trim());--------xxxxxxxxxxxxxxxxxxxxxx
kkje = Double.parseDouble(temp[10].trim());
yhbj = temp[11].trim();
s = br.readLine();
//更新 T_sbysmx信息
int i = dbc.updateTsbysmx(id, kksj, kkje, yhbj);
//查询银行端批量扣款成功信息
List<TSbysmx> list = dbc.findXjKkSj(sbh, hh);
for (int count = 0; count < list.size(); count++) {
TSbysmx ts = list.get(i);
//更新成功之后。把此条数据移到T——SBFRKMXB
dbc.intsertKkje(ts);
}
dbc.kkAfterDelete(sbh, hh);
//
......
}
第一行都能读取的信息
xxxxxxxxxxxxxxxx:java.lang.ArrayIndexOutOfBoundsException: 9
GH_SB_ PLKK_20081113171253.txt:
2|2334.0
377|10362187|郭春梅|1|1|11|179|200806-200806|20|20080808|200|11|113467497
381|10364549|朱永贵|12221212|11111111|11|2155|200707-200806|20|20080808|200|11|113467497
(Enter但无内容....)
这样的问题如何处理!
int totalCount = 0;
double totalMoney = 0.0d;
BufferedReader br = new BufferedReader(new FileReader("GH_SB_ PLKK_20081113171253.txt"));
String line = br.readLine();
if (line == null || (!line.contains("|"))){
return;
}
String[] strs = line.split("\\|");
totalCount = Integer.parseInt(strs[0].trim());
totalMoney = Double.parseDouble(strs[1]);
System.out.println(totalCount + " : " + totalMoney);
while((line = br.readLine()) != null){
if (line.contains("|")){
String[] record = line.split("\\|");
for (String word : record)
System.out.print(word);
System.out.println();
}
}
br.close();
}
那么我有个想法可以参考一下:使用文件读写的readline()方法,先按行读入到一个数组中
总笔数:arr(0).split("|")[0] 总金额:arr(0).split("|")[1]
文档号arr(1).split("|")[0],扣款金额arr(1).split("|")[10],
银行行号arr(0).split("|")[5],扣款时间arr(0).split("|")[9]