首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

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

大神们帮我看一下下面的程序有什么地方可以优化,并指出存在的问题
大神们帮我优化一下下面的程序,并指出存在的问题,(将两个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
}
}
}
}


[解决办法]
    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()方式,而不用自己重写

热点排行