这不是关于读或写的一般问题。我用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课程,但我们没有涉及它,但它是一个很棒的功能,感谢让我熟悉它!
RE:编辑。请注意,您也可以通过使用'TreeSet'而不是'HashSet'来完全剪切字符串数组 - 您可以使用for(String line:lines){'来迭代Set [(或任何'Iterable'))。为此,您需要使用泛型声明:'Set lines = new TreeSet ();' –
McDowell
2009-12-05 12:30:37