如题,有两个文本文件a.txt和b.txt;要删除b.txt中a.txt所包含的内容,生成c.txt。
如:a.txt内容如下:
12345
23456
78990
12342
...
b.txt内容如下:
12345
23456
78990
12342
34251
32587
78234
...
经过处理后,得到的c.txt内容如下:
34251
32587
78234
...a.txt的内容比b.txt要少很多,并且b.txt的内容在100万行左右!有没有什么高效的算法?谢谢!
如:a.txt内容如下:
12345
23456
78990
12342
...
b.txt内容如下:
12345
23456
78990
12342
34251
32587
78234
...
经过处理后,得到的c.txt内容如下:
34251
32587
78234
...a.txt的内容比b.txt要少很多,并且b.txt的内容在100万行左右!有没有什么高效的算法?谢谢!
解决方案 »
- 替换html文件中,的所有字颜色为红色\字体大小为16号\字体为宋体这个怎么做啊?用正则?
- 利用java 判断一段话中有某个词多少个
- 我在创建对象输入流时,卡住了,怎么也过不去!谁能帮帮我呀!
- 请问JAVA SWING能不能锁定整个WINDOWS页面?输入密码才能解锁
- 求达人知道读取数据问题..
- Vector可不可以用来保存类呢?
- javabean和class的区别是什么?
- java开发的c/s数据库程序是不是可以不安装数据库客户端?
- 哪位高人指点:JB如何与SQL连接???用JDBC-ODBC!!
- 在JAVA中如何实现类似VC中的多文档?
- GUI设计问题,在线等?
- Java源程序读不出来了???急救!!新手
如果是的话就用位图排序,首先读取b.txt文件构造bitmap,然后遍历a.txt并把重复的位置为0,最会输出bitmap到c.txt。
private static final int MAX = 100000;
private int[] bitmap = new int[MAX / 32 + 1]; public void filter() {
// read b.txt
File file = new File("c:\\b.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
int n = Integer.parseInt(line);
bitmap[n >>> 5] |= (1 << (n & 0X1F));
line = reader.readLine();
} } catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignoreWhenClose) {
}
}
} // read a.txt and iterate
file = new File("c:\\a.txt");
reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
int n = Integer.parseInt(line);
if ((bitmap[n >>> 5] & (1 << (n & 0X1F))) != 0) {
bitmap[n >>> 5] &= ~(1 << (n & 0X1F));
line = reader.readLine();
}
} } catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignoreWhenClose) {
}
}
} //
file = new File("c:\\c.txt");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < MAX; i++) {
if ((bitmap[i >>> 5] & (1 << (i & 0X1F))) != 0)
writer.append(String.valueOf(i)).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException ignoreWhenClose) {
}
}
}
} public static void main(String[] args) {
new Test().filter();
}
}
如果没有就输入到c中。b数据量比较大的话就应该直接文件操作。
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;public class DiffentContent {
/**
* 获得b.txt文件中a.txt没有的部分
*/
private List<String> Diffent(File a, File b) {
List<String> list = new ArrayList<String>();
try {
list = this.readB(b);// 得到要比较的b.txt中的内容
BufferedReader reader = new BufferedReader(new FileReader(a));// 把a.txt读入到缓存
String oneLine = "";
while ((oneLine = reader.readLine()) != null) {// 一行一行的读取
oneLine = oneLine.trim();// 去掉前后空格
if (list.contains(oneLine))//如果包含就移除
list.remove(oneLine);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 把B文件的没一行放入到List中
* @return
*/
private List<String> readB(File b) {
List<String> list = new ArrayList<String>();//要返回的b.txt文本中的内容
if(b != null) {
try {
BufferedReader reader = new BufferedReader(new FileReader(b));//把b.txt读入到缓存
String oneLine = "";
while((oneLine=reader.readLine()) != null){//一行一行的读取
list.add(oneLine.trim());//加入到List中
}
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
/**
* 把不同的内容写入到c.txt文件中
*/
private void writeC(List<String> different, File c) {
try {
if(different != null) {
FileWriter writer = new FileWriter(c);
for(String str : different) {
writer.append(str).append("\r\n");
}
writer.flush();
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//入口方法
public static void main(String[] args) {
File a = new File("c:/a.txt");//a.txt文件
File b = new File("c:/b.txt");//b.txt文件
File c = new File("c:/c.txt");//输出的c.txt文件
DiffentContent diffentContent = new DiffentContent();
List<String> diffent = diffentContent.Diffent(a, b);//获得b.txt文件中a.txt没有的部分
diffentContent.writeC(diffent, c);
}}
如:
select num from a where a.num not in (select num from b);
这里的a表就是a.txt的内容,b就是b.txt的内容
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;public class DiffentContent {
/**
* 获得b.txt文件中a.txt没有的部分
*/
private List<String> Diffent(File a, File b) {
List<String> list = new ArrayList<String>();
try {
list = this.readB(b);// 得到要比较的b.txt中的内容
BufferedReader reader = new BufferedReader(new FileReader(a));// 把a.txt读入到缓存
String oneLine = "";
while ((oneLine = reader.readLine()) != null) {// 一行一行的读取
oneLine = oneLine.trim();// 去掉前后空格
if (list.contains(oneLine))//如果包含就移除
list.remove(oneLine);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 把B文件的没一行放入到List中
* @return
*/
private List<String> readB(File b) {
List<String> list = new ArrayList<String>();//要返回的b.txt文本中的内容
if(b != null) {
try {
BufferedReader reader = new BufferedReader(new FileReader(b));//把b.txt读入到缓存
String oneLine = "";
while((oneLine=reader.readLine()) != null){//一行一行的读取
list.add(oneLine.trim());//加入到List中
}
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
/**
* 把不同的内容写入到c.txt文件中
*/
private void writeC(List<String> different, File c) {
try {
if(different != null) {
FileWriter writer = new FileWriter(c);
for(String str : different) {
writer.append(str).append("\r\n");
}
writer.flush();
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//入口方法
public static void main(String[] args) {
File a = new File("c:/a.txt");//a.txt文件
File b = new File("c:/b.txt");//b.txt文件
File c = new File("c:/c.txt");//输出的c.txt文件
DiffentContent diffentContent = new DiffentContent();
List<String> diffent = diffentContent.Diffent(a, b);//获得b.txt文件中a.txt没有的部分
diffentContent.writeC(diffent, c);
}}