一个面试时的机试题
有一段数据,共15行6列,内容如下:
7272, "车江 ", "转扣预存 ",200704, "衡南县 ",+0000000022675.73
7273, "车江 ", "转扣预存 ",200704, "衡南县 ",+0000000011322.80
7273, "车江 ", "转扣预存 ",200704, "衡南县 ",7322.80
7283, "向阳桥 ", "转扣预存 ",200704, "衡南县 ",-0000033289.55
7283, "向阳桥 ", "转扣预存 ",200704, "衡南县 ",289.55
7291, "三塘 ", "转扣预存 ",200704, "衡南县 ",+0000000037059.34
7291, "三塘 ", "转扣预存 ",200704, "衡南县 ",-12268.84
7292, "三塘 ", "转扣预存 ",200704, "衡南县 ",+0000000016902.98
12043, "廖田 ", "转扣预存 ",200704, "衡南县 ",aafd
12043, "廖田 ", "转扣预存 ",200704, "衡南县 ",-9809
12043, "廖田 ", "转扣预存 ",200704, "衡南县 ",+0000000000212.04
12051, "江口 ", "转扣预存 ",200704, "衡南县 ",+0000000030441.11
12051, "江口 ", "转扣预存 ",200704, "衡南县 ",26841.11
12051, "江口 ", "转扣预存 ",200704, "衡南县 ",+0000000030441.11
12052, "江口 ", "转扣预存 ",200704, "衡南县 ",02e574.79
将该数据存储在文本文件A中
要求读入该文件,然后把第6列是正确数值的行取出,按第2列升序排列,再写入到文件B中;把第6列不是正确数值的行写入到文件C中
------解决方案--------------------没测试过,只是编译过去了,你调试一下!
import java.io.*;
import java.util.*;
public class TeleRecProc {
/**
* @param args
*/
String correctRecPath;
String wrongRecPath;
BufferedWriter correctRecWriter;
BufferedWriter wrongRecWriter;
LinkedList <String> correctRecSet;
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length <3){
System.out.println( "Usage:TeleRecProc InputData GoodRecRepository BadRecRepository ");
System.exit(-1);
}
try{
BufferedReader br;
br=new BufferedReader(new InputStreamReader(new FileInputStream(args[1])));
String rec;
TeleRecProc teleRecProc=new TeleRecProc();
while((rec=br.readLine())!=null){
if(teleRecProc.isRecCorrect(rec)) teleRecProc.insertCorrectRecSet(rec);
else teleRecProc.writeWrongRec(rec);
}
teleRecProc.sortRec();
teleRecProc.cleanUp();
}catch(
IOException ex){
}
}
boolean isRecCorrect(String rec)
{
rec=rec.trim();
rec=rec.substring(rec.lastIndexOf((int) ', ')+1);
try{
Float.parseFloat(rec);
}catch(
NumberFormatException ex){
return false;
}
return true;
}
void writeCorrectRec(LinkedList <String> recSet)throws IOException
{
correctRecWriter=new BufferedWriter(
new OutputStreamWriter
(new FileOutputStream
(correctRecPath,false)
)
);
Iterator <String> it=recSet.iterator();
while(it.hasNext()){
String rec = it.next().toString();
correctRecWriter.write(rec+ "\n ");
}
}
void writeWrongRec(String rec)throws IOException
{
if(wrongRecWriter==null)
wrongRecWriter=new BufferedWriter(
new OutputStreamWriter
(new FileOutputStream
(wrongRecPath,false)
)
);
wrongRecWriter.write(rec+ "\n ");