大神们帮我看一下下面的程序有什么地方可以优化,并指出存在的问题

黑夜中的一点慰藉 2013-01-13 01:32:32
大神们帮我优化一下下面的程序,并指出存在的问题,(将两个txt文件内容分别去重并去除重复内容后写入到另外两个txt文件,)
package com.wwy.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CopyFile {

/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// TODO Auto-generated method stub

List<List> mylist = read();
List<List> my = read();
List<String> l1 = repetition(mylist.get(0), mylist.get(1));
List<String> l2 = repetition(my.get(1),my.get(0));
write(l1,l2);
System.out.println("ok");
}

public static List<List> read(){
File f1 = new File("D:\\a\\test1.txt");
File f2 = new File("D:\\a\\test2.txt");
List<List> mylist = new ArrayList<List>();
List<String> l1 = new ArrayList<String>();
List<String> l2 = new ArrayList<String>();
String str1 = "";
String str2 = "";
BufferedReader br1 = null;
BufferedReader br2 = null;
try {
br1 = new BufferedReader(new FileReader(f1));
br2 = new BufferedReader(new FileReader(f2));
while((str1 = br1.readLine() ) != null ){// (str2 = br2.readLine()) != null){
l1.add(str1);
}
while((str2 = br2.readLine()) != null){
l2.add(str2);
}
mylist.add(l1);
mylist.add(l2);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(br1 != null){
br1.close();
br1 = null;
}
if(br2 != null){
br2.close();
br2 = null;
}
} catch (Exception e2) {
// TODO: handle exception
}
}
return mylist;
}

public static List<String> repetition(List<String> l1,List<String> l2){
for(int i = 0; i < l1.size(); i++){
for(int j = 0;j<l2.size();j++){
if(l2.get(j) .equals(l1.get(i))){
System.out.println("sdgsad======" + l1.get(i));
l1.remove(i);
}
}

}
/*for(String str1 :l1){
System.out.println("str1 : " + str1);
for(String str2 : l2){
System.out.println("str2 : " + str2);
if(str1.equals(str2)){
System.out.println("222 2 " + str1);
//int index = l1.indexOf(str1);
l1.remove(str1);
System.out.println("============ " + l1.remove(str1) );
}
}
}*/
return l1;
}

public static void write(List<String> l1,List<String> l2){
File f1 = new File("D:\\a\\result1.txt");
File f2 = new File("D:\\a\\result2.txt");
BufferedWriter bw1 = null;
BufferedWriter bw2 = null;
try {
bw1 = new BufferedWriter(new FileWriter(f1));
bw2 = new BufferedWriter(new FileWriter(f2));
for(int i = 0;i < l1.size();i++){
bw1.write(l1.get(i) + "\n");
}
for(int i = 0;i < l1.size();i++){
bw2.write(l2.get(i) + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(bw1 != null){
bw1.close();
bw1 = null;
}
if(bw2 != null){
bw2.close();
bw2 = null;
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
...全文
109 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 Inhibitory 的回复:
Java code?123456789101112131415161718192021222324 public static List<String> repetition(List<String> l1,List<String> l2){ for(int i = 0; i < l1.size(); i++){ for(int ……
谢谢!以前没注意这个方法,这样确实简化了很多了,但是我更想知道了集合方面有没有优化的,感觉集合那部分运用太别扭了,集合是不是有修改记忆功能??
Inhibitory 2013-01-13
  • 打赏
  • 举报
回复
    public static List<String> repetition(List<String> l1,List<String> l2){
        for(int i = 0; i < l1.size(); i++){
            for(int j = 0;j<l2.size();j++){
                if(l2.get(j) .equals(l1.get(i))){
                    System.out.println("sdgsad======" + l1.get(i));
                    l1.remove(i);
                }
            }
             
        }
        /*for(String str1 :l1){
            System.out.println("str1 : " + str1);
             for(String str2 : l2){
                 System.out.println("str2 : " + str2);
                 if(str1.equals(str2)){
                     System.out.println("222 2 " + str1);
                     //int index = l1.indexOf(str1);
                     l1.remove(str1);
                     System.out.println("============ " + l1.remove(str1) );
                 }
             }
        }*/
        return l1;
    }
去重复这一段代码可以直接用List.removeAll()方式,而不用自己重写

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧