我的文件名是:
20050701.txt
20050702.txt
20050703.txt
20050704.txt
20050705.txt
20050706.txt
20050707.txt
第一个文件内容是:
ST:20050701 000011 TI:20050719 000011 CN:06 LT:10 PN:13099885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050701 000011 TI:20050719 000032 CN:06 LT:90 PN:13099885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!
ST:20050701 000011 TI:20050719 000011 CN:06 LT:10 PN:13299885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050701 000011 TI:20050719 000032 CN:06 LT:90 PN:13299885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!第二个文件内容是:
ST:20050702 000011 TI:20050719 000011 CN:06 LT:10 PN:13099885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050702 000011 TI:20050719 000032 CN:06 LT:90 PN:13099885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!
ST:20050702 000011 TI:20050719 000011 CN:06 LT:10 PN:13099885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050702 000011 TI:20050719 000032 CN:06 LT:90 PN:13099885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!依次类推
我想用JAVA自己输入两个文件名,查出这两个文件之间所有的内容带LT:90的所有手机号码,重复的手机号只写一遍.写入一个新的文本,文件名不能写死!
(就是我输入:20050701 和 20050702
打印出
PN:13099885853 PN:13299885853 PN:13099885853 )写到新的文本也是这样谢谢了,最好能给出源代码!
20050701.txt
20050702.txt
20050703.txt
20050704.txt
20050705.txt
20050706.txt
20050707.txt
第一个文件内容是:
ST:20050701 000011 TI:20050719 000011 CN:06 LT:10 PN:13099885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050701 000011 TI:20050719 000032 CN:06 LT:90 PN:13099885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!
ST:20050701 000011 TI:20050719 000011 CN:06 LT:10 PN:13299885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050701 000011 TI:20050719 000032 CN:06 LT:90 PN:13299885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!第二个文件内容是:
ST:20050702 000011 TI:20050719 000011 CN:06 LT:10 PN:13099885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050702 000011 TI:20050719 000032 CN:06 LT:90 PN:13099885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!
ST:20050702 000011 TI:20050719 000011 CN:06 LT:10 PN:13099885853 DN:1015902210 AR:D KC:- RS:@!!!!!!!
ST:20050702 000011 TI:20050719 000032 CN:06 LT:90 PN:13099885853 DN:1015902210 AR:D KC:* RS:@!!!!!!!依次类推
我想用JAVA自己输入两个文件名,查出这两个文件之间所有的内容带LT:90的所有手机号码,重复的手机号只写一遍.写入一个新的文本,文件名不能写死!
(就是我输入:20050701 和 20050702
打印出
PN:13099885853 PN:13299885853 PN:13099885853 )写到新的文本也是这样谢谢了,最好能给出源代码!
import java.util.*;public class FindLine {
private File file;
private static final String TOFIND = "LT:90";
private Collection <String>result;
/**
* constructor
* @param fpath the specified file (path)name
*/
public FindLine(String fpath){
file = new File(fpath);
result = new ArrayList<String>();
}
/**
* perform the function of finding the line which containing the specified
* string
* @param str the specified string contained in the line
* @return the line that contains the specified str
* or null,if no line contains such string
*/
public Collection find(String str){
try{
BufferedReader br = new BufferedReader(new FileReader(file));
String tmp = new String();
while((tmp=br.readLine())!=null){
if(tmp.contains(str)){
//return tmp;
result.add(tmp);
}
}
}catch(IOException e){
e.printStackTrace();
}
return result;
}
/**
* performed the find function,the specified string is initialized to
* "LT:90"
* @return the line that contains the specified str
* or null,if no line contains such string
*/
public Collection find(){
return find(TOFIND);
}
/**
* the main function
* @param args ...
*/
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = new String();
System.out.print("intput the file name: ");
try{
name = br.readLine();
if(name.lastIndexOf(".txt")==-1){
name += ".txt";
}
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
System.out.println(new FindLine(".\\"+name).find());
}
}
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(".\\"+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[]){
/*String file1 = "20050701.txt";
String file2 = "20050702.txt";
String fileStore = "telnum.txt";*/
String file1 = new String();
String file2 = new String();
String fileStore = new String();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("file1: ");
try{
file1 = br.readLine();
if(file1.lastIndexOf(".txt")==-1){
file1 += ".txt";
}
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
System.out.print("file2: ");
try{
file2 = br.readLine();
if(file2.lastIndexOf(".txt")==-1){
file2 += ".txt";
}
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
System.out.print("fileStore: ");
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);
}
}
~~~~~~~~
机号只写一遍.写入一个新的文本,文件名不能写死!---------------------------------------------------------------------------------------
楼主是做什么的?怎么有那么多手机号码, 不会是那种专门发中奖信息的骗子吧?
大家不要给这种人写代码, 小心上当.
import java.util.StringTokenizer;
import java.util.LinkedList;
class Read {
public Read() {
try{
int num = 2;
LinkedList phone = new LinkedList();
String file[] = new String[num];
BufferedReader reader[] =new BufferedReader[num];
BufferedReader in[] = new BufferedReader[num];
for(int i = 0;i<num;i++) {
System.out.print("File" + i +":");
in[i] = new BufferedReader(
new InputStreamReader(System.in));
file[i] = in[i].readLine();
reader[i] = new BufferedReader(
new FileReader(file[i]+".txt"));
}
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter("new.txt",true)));
for(int t = 0;t<num;t++) {
while(true) {
String line = reader[t].readLine();
if(line!=null){
StringTokenizer s = new StringTokenizer(line);
line = s.nextToken();
while(!line.equals("LT:90")){
if(!s.hasMoreTokens()){
break;
}
line = s.nextToken();
}
if(line.equals("LT:90")) {
if(!s.hasMoreTokens()){
break;
}
line = s.nextToken();
if(line.startsWith("PN:")) {
boolean isRepeat = false;
for(int i = 0; i<phone.size();i++) {
if(line.equals(String.valueOf(phone.get(i)))){
isRepeat = true;
break;
}
}
if(isRepeat==false) {
out.print(line+" ");
phone.add(line);
}
}
}
}
else {
break;
}
}
}
out.close();
}catch(IOException e){
}
}
public static void main(String [] args) {
Read e = new Read();
}
}