目的为从文件中读取数据做排序:
结果:
入参
A文件:
B文件:
代码:
//存活动id号
List<String> acionlist = new ArrayList<>();
//存放数据
Map<String,List<String>> map = new HashMap<>();
BufferedReader bufferedReader = new BufferedReader(new FileReader("acionID.txt"));
BufferedReader bufferedReader1 = new BufferedReader(new FileReader("acionID1.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("acionID.dat"));
String len = null;
String len1 = null;
while ((len = bufferedReader.readLine()) != null) {
acionlist.add(len);
}
bufferedReader.close();
while ((len1 = bufferedReader1.readLine()) != null) {
ListIterator<String> stringListIterator = acionlist.listIterator();
while (stringListIterator.hasNext()) {
String vaule = stringListIterator.next();
map.put(len1,acionlist);
}
}
bufferedReader1.close();
long timeStart = System.currentTimeMillis();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
for (String v : entry.getValue()) {
// System.out.println(entry.getKey()+"---->"+v);
bufferedWriter.write(entry.getKey()+","+v);
//换行
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
bufferedWriter.close();
long timeEnd = System.currentTimeMillis();
System.out.println("总共花费:" + (timeEnd - timeStart) + "ms");
System.out.println("写入完毕!");
开发哥们给的提示:
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
Map<String,List<String>> map = new HashMap<>();
map.put("1",list);
map.put("2",list);
map.put("3",list);
map.put("4",list);
for(Map.Entry<String,List<String>> m : map.entrySet()){
for(String v : m.getValue()){
System.out.println(m.getKey()+"---->"+v);
}
}
java改进版本:
long timeStart = System.currentTimeMillis();
BufferedReader activity_ID = new BufferedReader(new FileReader("activity_ID.txt")); //活动di
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("acionID.dat"));
String len = null;
String len1 = null;
while ((len = activity_ID.readLine()) != null) {
BufferedReader cookice_id = new BufferedReader(new FileReader("cookice_id.txt"));
while ((len1 = cookice_id.readLine()) != null) {
bufferedWriter.write(len + "," + len1);
bufferedWriter.newLine();
}
cookice_id.close();
}
activity_ID.close();
bufferedWriter.close();
long timeEnd = System.currentTimeMillis();
System.out.println("总共花费:" + (timeEnd - timeStart) + "ms");
System.out.println("操作完毕!");
}
python使用时间:
start_time = time.time()
f = open("a.txt", "r")
f1 = open("b.txt", "r")
f2 = open("c.txt", "w")
# for i in range(10):
# f1.write(str(i) + '\n')
# f1.close()
for i in f.read().split():
for j in f1.read().split():
f2.write(i+","+j+"\n")
# print(i+","+j+"\n")
f1.seek(0)
f.close()
f1.close()
f2.close()
end_time = time.time()
print("使用多少时间:!%s"%(end_time-start_time))
转载于:https://blog.51cto.com/357712148/2347821