Vector vector1 = new Vector();// //定义容器类对象
Vector vector2 = new Vector();// //定义容器类对象 for (int i = 0; i < vector1.size(); i++) {
for (int j = 0; j < vector2.size(); j++) {你们还没有个容器里面写入任何东西呢,2个for循环永远都不会进,所以2容器永远是空。建议先对该容器初始化

解决方案 »

  1.   


    package csdn.bugs;import java.util.*;
    import java.io.*;public class ResortByDel { @SuppressWarnings("unchecked")
    public void ResortToTemp(String firstFile, String secondFile,
    String tempPath1, String tempPath2) throws IOException {
    // String oldPath = oldFolderPath;
    File f1 = new File(firstFile);
    File f2 = new File(secondFile);
    String str1 = null;
    String str2 = null;
    String string1 = null;
    String string2 = null;
    Vector vector1 = new Vector();// //定义容器类对象
    Vector vector2 = new Vector();// //定义容器类对象
    // boolean IsRepeat = false;
    try {
    BufferedReader reader1 = new BufferedReader(new FileReader(f1));
    BufferedReader reader2 = new BufferedReader(new FileReader(f2));
    while ((str1 = reader1.readLine()) != null
    && (str2 = reader2.readLine()) != null) {
    if(!vector1.contains(str1)){
    vector1.add(str1);
    }
    if(!vector2.contains(str2)){
    vector2.add(str2);
    }
    }
    reader1.close();
    reader2.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    // 写入文件
    String newFile1 = tempPath1;
    String newFile2 = tempPath2;
    File tempFile1 = new File(newFile1);
    File tempFile2 = new File(newFile2);
    if (tempFile1.exists()) {
    tempFile1.delete();
    }
    if (tempFile2.exists()) {
    tempFile2.delete();
    }
    tempFile1.createNewFile();
    tempFile2.createNewFile();
    try {
    BufferedWriter writer1 = new BufferedWriter(new FileWriter(
    tempFile1, true));
    BufferedWriter writer2 = new BufferedWriter(new FileWriter(
    tempFile2, true));
    for (int i = 0; i < vector1.size(); i++) {
    str1 = (String) vector1.elementAt(i);
    writer1.write(str1);
    writer1.newLine();
    writer1.flush();
    }
    for (int i = 0; i < vector2.size(); i++) {
    str2 = (String) vector2.elementAt(i);
    writer2.write(str2);
    writer2.newLine();
    writer2.flush();
    }
    writer1.close();
    writer2.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) throws IOException {
    String tempPath1 = "D:\\tmp\\test1.txt";
    String tempPath2 = "D:\\tmp\\test2.txt";
    // String sourceFolder = "D:/tmp/tmp";
    String resultPath1 = "D:\\tmp\\result1.txt";
    String resultPath2 = "D:\\tmp\\result2.txt";
    // File tempFile = new File(tempPath);
    ResortByDel resort = new ResortByDel();
    // resort.AllResortToTemp(sourceFolder,tempPath);//首先放到temp.txt,然后删除
    resort.ResortToTemp(tempPath1, tempPath2, resultPath1, resultPath2);// 把temp文件再去重放入result文件
    // resort.DelTemp(tempPath);//删除temp.txt中转文件
    System.out.println("OK"); }}
    这样写 
      

  2.   

    不过 你不就是想实现2文件拷贝么,直接拷贝就OK了,干嘛读出来放内存 然后再写呢。
    你干嘛用vector呢,建议楼主看看vector与ArrayList的区别,看看Synchronized。
      

  3.   

    用ArrayList也可以的,我懒得折腾了
      

  4.   

    不好意思,我觉得用这句去重不太好,是把完全重复的去掉,而这两句把部分重复的也去掉了
     if(!vector1.contains(str1)){
                        vector1.add(str1);
                    }
                    if(!vector2.contains(str2)){
                        vector2.add(str2);
                    }
      

  5.   

    你要是有顺序要求又想去重复就用LinkedHashSet吧,去什么空格可以用正则"\\s*"
      

  6.   

    看看我这样写可以不?
    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
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub List<List> mylist = read();
    List<String> l1 = repetition(mylist.get(0),mylist.get(1));
    List<String> l2 = repetition(mylist.get(1),mylist.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){
    String str1 = "";
    String str2 = "";
    for(int i = 0; i < l1.size();i++){
    str1 = l1.get(i).trim();
     for(int j = 0;j < l2.size(); j++){
     str2 = l2.get(j).trim();
     if(str1.equals(str2)){
     l1.remove(i);
     }
     }
    }
    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
    }
    }
    }
    }
      

  7.   

    不好意思上面的有点问题,没注意集合存储问题,抱歉,不过代码还是有好多问题,请大家指正
    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
    }
    }
    }
    }