2009-12-05 39 views
1

这不是关于读或写的一般问题。我用Java编写了一个程序,用于从图像中读取一些元数据的文本文件。它们包含名称和长长的名单,有时超过4000个名字。不幸的是,这些名称中的许多名称都是相同的,所以我编写了一个程序,该列表以.txt文件形式提供,并清除重复项,并将新清理后的按字母顺序排序的列表输出到输出txt文件。此外,该程序将HTML列表标签添加到每个名称,以便我可以将它们复制粘贴到任何需要的位置。阅读和写出Java程序问题

示例文本文件

健谈的小猫咪
健谈的小猫咪
Bearly NUF塔兹
了律碱式

等等,等等

你可以看到我使用了一个在这里测试http://www.megaupload.com/?d=WNXYVHEN

但是,它似乎不能正常工作因为我的输出文件中仍然有重复项。然而,我写给我的代码似乎是正确的,这就是为什么我问是否存在与我如何设置我的读写问题。

我的代码

/* * This program takes in a text file that has a bunch of words listed. It then creates a single alphabetically * organized html list from that data. It also strips the data of dupblicates. */

import java.io.*; import java.util.Arrays;

public class readItWriteIt {
public static void main(String args[]) { int MAX = 10000; String[] lines = new String[MAX]; boolean valid = true;

try{ 
    //Set up Input 
    FileInputStream fstream = new FileInputStream("test.txt"); 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 


    //Set up Output 
    FileWriter ostream = new FileWriter("out.txt"); 
    BufferedWriter out = new BufferedWriter(ostream); 

    //counters 
    int count = 0; 
    int second_count = 0; 

    //start reading in lines from the file 
    while ((strLine = br.readLine()) != null){ 

    //check to make sure that there aren't duplicates. If a line is the same as another line 
    //set boolean valid to false else set to true. 
    if((second_count++ > 0) && (count > 0)){ 
     for(int i=0; i < count; i++) 
     { 
      if(lines[i].equals(strLine)){ 
       valid = false; 
      } 
      else 
      { 
       valid = true; 
      } 
     } 
    } 


    //only copy the line to the local array if it is not a duplicate. Else do nothing with it. 
     if (valid == true){ 
      lines[count] = strLine.trim(); 
      count++; 
     } 
     else{} 
     second_count++; 
    } 

    //create a second array so that you can get rid of all the null values. It is the size of the 
    //used length in the first array called "lines" 
    String[] newlines = new String[count]; 

    //copy data from array lines to array called newlines 
    for(int i = 0; i < count; i++){ 
     newlines[i] = lines[i]; 
    } 

    //sort the array alphabetically 
    Arrays.sort(newlines); 

    //write it out to file in alphabetical order along with the list syntax for html 
    for(int i = 0; i < count; i++) 
    { 
     out.write("<li>" + newlines[i] + "</li>"); 
     out.newLine(); 
    } 

    //close I/O 
    in.close(); 
    out.close(); 

    }catch (Exception e){//Catch exception if any 
     System.err.println("Error: " + e.getMessage()); 
    } 
    } 

}

我希望有人能帮助我。非常感谢! :)

嘿家伙感谢您的建议和帮助。 我写这样的

import java.util.HashSet; import java.util.Set; import java.io.*; import java.util.Arrays;

public class converter { public static void main(String[] args) {

try{ 
    //Set up Input 
    FileInputStream fstream = new FileInputStream("test.txt"); 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 

    //Set up Output 
    FileWriter ostream = new FileWriter("out.txt"); 
    BufferedWriter out = new BufferedWriter(ostream); 

    Set lines = new HashSet(); 
    boolean result; 

    while ((strLine = br.readLine()) != null){ 
     result = lines.add(strLine.trim()); 
    } 
    String[] newlines = new String[lines.size()]; 
    lines.toArray(newlines); 

    Arrays.sort(newlines); 

    //write it out to file in alphabetical order along with the list syntax for html 
    for(int i = 0; i < lines.size(); i++) 
    { 
     out.write("<li>" + newlines[i] + "</li>"); 
     out.newLine(); 
    } 

    out.close(); 
    in.close(); 

    }catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
    } 
} 

}

但由于其ewernli现在更有效率。我不知道关于集合,因为我刚刚参加了我的第一个Java课程,但我们没有涉及它,但它是一个很棒的功能,感谢让我熟悉它!

+0

RE:编辑。请注意,您也可以通过使用'TreeSet'而不是'HashSet'来完全剪切字符串数组 - 您可以使用for(String line:lines){'来迭代Set [(或任何'Iterable'))。为此,您需要使用泛型声明:'Set lines = new TreeSet ();' – McDowell 2009-12-05 12:30:37

回答

1

如果将行添加到Set(作为键)而不是Array中,您会发现不需要执行任何重复处理。它会被照顾好,你的程序会更简单和更短。

0

实际上你的代码需要一些改进, 但是我觉得最大的错误是与未修剪的字符串进行比较,然后使用修剪后的字符串将其放到行数组中。

lines[i].equals(strLine) // instead use "lines[i].equals(strLine.trim())" 
1

数组不是你想要的数据结构(你是否需要一个固定长度的数据结构,但有可变元素?)。看看java.util中的收集类型。尤其要看SortedSet的实现,如TreeSet。这将:

  1. 展开来保存数据
  2. 消除重复(它是一个Set
  3. 排序的内容为你添加它们(见Comparator实现比如String.CASE_INSENSITIVE_ORDER