import java.io.*;
import java.util.*;public class TelNumStore
{
private Collection c1,c2;
private static final String STR = "LT:90";
private HashSet<String> telNumSet;
public TelNumStore(String file1, String file2)
{
telNumSet = new HashSet<String>();
c1 = new FindLine(file1).find();
c2 = new FindLine(file2).find();
}
public void storeTelnum(String file) { FileOutputStream fos=null;
try
{
fos = new FileOutputStream(new File("e:\\"+file));
}
catch(IOException e)
{
e.printStackTrace();
}
findTelNum(c1);
findTelNum(c2);
System.out.println(telNumSet);
Iterator it = telNumSet.iterator();
while(it.hasNext())
{
String str = (String)it.next();
try
{
fos.write(str.getBytes(),0,str.getBytes().length);
fos.write((byte)'\t');
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void findTelNum(Collection c)
{
Iterator it = c.iterator();
while(it.hasNext())
{
StringTokenizer st = new StringTokenizer((String)it.next()," ");
while(st.hasMoreTokens())
{
String str = st.nextToken();
if(str.contains("PN:"))
{
telNumSet.add(str);
}
}
}
}
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String file1 = "";
System.out.print("开始日期:");
System.out.println(file1);
try
{
file1 = br.readLine();
if(file1.lastIndexOf(".txt")==-1)
{
file1 += ".txt";
}
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
String file2 = "";
System.out.print("结束日期:");
try
{
file2 = br.readLine();
if(file2.lastIndexOf(".txt")==-1)
{
file2 += ".txt";
}
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
String fileStore = "";
System.out.print("存储的文件名:");
try
{
fileStore = br.readLine();
if(fileStore.lastIndexOf(".txt")==-1)
{
fileStore += ".txt";
}
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
new TelNumStore(file1,file2).storeTelnum(fileStore);
}
}
文件名20050701.TXT....
文件内容
ST:20050701 005509 TI:20050701 005509 CN:20 LT:10 PN:13464213270 DN:1015902210 AR:A KC:-
ST:20050701 005509 TI:20050701 005548 CN:20 LT:90 PN:13464210723 DN:1015902210 AR:D KC:-
...............
我想再加一个输入判断条件,就是AR:的类别,A-Z的
就是我输入,两个文件名,输入AR后面的字母,根据这3个条件查出 PN:13464210723
现在程序的问题是,不能查两个文件之间的内容,而是只查输入的那两个文件,然后是没有AR条件判断,希望高手能给改改,谢谢声明一下,我是做SP的,主要是用手机买彩票,不是什么骗子
import java.util.*;public class TelNumStore
{
private Collection c1,c2;
private static final String STR = "LT:90";
private HashSet<String> telNumSet;
public TelNumStore(String file1, String file2)
{
telNumSet = new HashSet<String>();
c1 = new FindLine(file1).find();
c2 = new FindLine(file2).find();
}
public void storeTelnum(String file) { FileOutputStream fos=null;
try
{
fos = new FileOutputStream(new File("e:\\"+file));
}
catch(IOException e)
{
e.printStackTrace();
}
findTelNum(c1);
findTelNum(c2);
System.out.println(telNumSet);
Iterator it = telNumSet.iterator();
while(it.hasNext())
{
String str = (String)it.next();
try
{
fos.write(str.getBytes(),0,str.getBytes().length);
fos.write((byte)'\t');
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void findTelNum(Collection c)
{
Iterator it = c.iterator();
while(it.hasNext())
{
StringTokenizer st = new StringTokenizer((String)it.next()," ");
while(st.hasMoreTokens())
{
String str = st.nextToken();
if(str.contains("PN:"))
{
telNumSet.add(str);
}
}
}
}
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String file1 = "";
System.out.print("开始日期:");
System.out.println(file1);
try
{
file1 = br.readLine();
if(file1.lastIndexOf(".txt")==-1)
{
file1 += ".txt";
}
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
String file2 = "";
System.out.print("结束日期:");
try
{
file2 = br.readLine();
if(file2.lastIndexOf(".txt")==-1)
{
file2 += ".txt";
}
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
String fileStore = "";
System.out.print("存储的文件名:");
try
{
fileStore = br.readLine();
if(fileStore.lastIndexOf(".txt")==-1)
{
fileStore += ".txt";
}
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
new TelNumStore(file1,file2).storeTelnum(fileStore);
}
}
文件名20050701.TXT....
文件内容
ST:20050701 005509 TI:20050701 005509 CN:20 LT:10 PN:13464213270 DN:1015902210 AR:A KC:-
ST:20050701 005509 TI:20050701 005548 CN:20 LT:90 PN:13464210723 DN:1015902210 AR:D KC:-
...............
我想再加一个输入判断条件,就是AR:的类别,A-Z的
就是我输入,两个文件名,输入AR后面的字母,根据这3个条件查出 PN:13464210723
现在程序的问题是,不能查两个文件之间的内容,而是只查输入的那两个文件,然后是没有AR条件判断,希望高手能给改改,谢谢声明一下,我是做SP的,主要是用手机买彩票,不是什么骗子
import java.util.*;public class TelNumStore {
//private Collection c1,c2;
private Collection[] files;
private static final String STR = "LT:90";
private HashSet<String> telNumSet;
private String AR;//specify the AR
public TelNumStore(String f[],String ar){
files = new Collection[f.length];
telNumSet = new HashSet<String>();
//c1 = new FindLine(file1).find();
//c2 = new FindLine(file2).find();
for(int i=0; i<files.length; i++){
f[i] = fileSuffixCheck(f[i],".txt");
files[i] = new FindLine(f[i]).find();
}
AR = ar;
}
public void storeTelnum(String file){
FileOutputStream fos=null;
file = fileSuffixCheck(file,".txt");
try{
fos = new FileOutputStream(new File(".\\"+file));
}catch(IOException e){
e.printStackTrace();
}
//findTelNum(c1);
//findTelNum(c2);
for(int i=0; i<files.length; i++){
findTelNum(files[i],AR);
}
System.out.println(telNumSet);
Iterator it = telNumSet.iterator();
while(it.hasNext()){
String str = (String)it.next();
try{
fos.write(str.getBytes(),0,str.getBytes().length);
fos.write((byte)'\t');
}catch(Exception e){
e.printStackTrace();
}
}
}
public void findTelNum(Collection c,String ar){
Iterator it = c.iterator();
String maybe = null;
boolean matchAR = false;
while(it.hasNext()){
StringTokenizer st = new StringTokenizer((String)it.next()," ");
while(st.hasMoreTokens()){
String str = st.nextToken();
if(str.contains("PN:")){
//telNumSet.add(str);
maybe = str;
}
if( str.contains("AR:"+ar))
matchAR = true;
}
}
if(matchAR)
telNumSet.add(maybe);
}
private String fileSuffixCheck(String fileName,String suf){
if(fileName.lastIndexOf(suf)==-1)
return fileName+suf;
return fileName;
}
//--------------------------------------
public static void main(String args[]){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int fileNum=0;
String files[]=null;
String fileStore=null;
String AR = null;
System.out.print("how manys files to be choosen? ");
try{
fileNum = Integer.parseInt(br.readLine());
files = new String[fileNum];
}catch(Exception e){
e.printStackTrace();
System.out.println("exiting...");
System.exit(0);
}
for(int i=0; i<fileNum; i++){
System.out.print("file"+i+": ");
try{
files[i] = br.readLine();
}catch(Exception e){
e.printStackTrace();
System.out.println("exiting...");
System.exit(0);
}
}
System.out.print("fileStore: ");
try{
fileStore = br.readLine();
}catch(Exception e){
e.printStackTrace();
}
System.out.print("AR: ");
try{
AR = br.readLine();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
new TelNumStore(files,AR).storeTelnum(fileStore);
}
}