回答
public class UniqueLineReader extends BufferedReader {
Set<String> lines = new HashSet<String>();
public UniqueLineReader(Reader arg0) {
super(arg0);
}
@Override
public String readLine() throws IOException {
String uniqueLine;
if (lines.add(uniqueLine = super.readLine()))
return uniqueLine;
return "";
}
//for testing..
public static void main(String args[]) {
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(
"test.txt");
UniqueLineReader br = new UniqueLineReader(new InputStreamReader(fstream));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
if (strLine != "")
System.out.println(strLine);
}
// Close the input stream
in.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
修改的版本:
public class UniqueLineReader extends BufferedReader {
Set<String> lines = new HashSet<String>();
public UniqueLineReader(Reader arg0) {
super(arg0);
}
@Override
public String readLine() throws IOException {
String uniqueLine;
while (lines.add(uniqueLine = super.readLine()) == false); //read until encountering a unique line
return uniqueLine;
}
public static void main(String args[]) {
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(
"/home/emil/Desktop/ff.txt");
UniqueLineReader br = new UniqueLineReader(new InputStreamReader(fstream));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println(strLine);
}
// Close the input stream
in.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
使用BufferedReader读取文本文件并将其存储在LinkedHashSet中。打印出来。
下面是一个例子:
public class DuplicateRemover {
public String stripDuplicates(String aHunk) {
StringBuilder result = new StringBuilder();
Set<String> uniqueLines = new LinkedHashSet<String>();
String[] chunks = aHunk.split("\n");
uniqueLines.addAll(Arrays.asList(chunks));
for (String chunk : uniqueLines) {
result.append(chunk).append("\n");
}
return result.toString();
}
}
这里的一些单元测试,以验证(忽略我的邪恶复制粘贴;)):
import org.junit.Test;
import static org.junit.Assert.*;
public class DuplicateRemoverTest {
@Test
public void removesDuplicateLines() {
String input = "a\nb\nc\nb\nd\n";
String expected = "a\nb\nc\nd\n";
DuplicateRemover remover = new DuplicateRemover();
String actual = remover.stripDuplicates(input);
assertEquals(expected, actual);
}
@Test
public void removesDuplicateLinesUnalphabetized() {
String input = "z\nb\nc\nb\nz\n";
String expected = "z\nb\nc\n";
DuplicateRemover remover = new DuplicateRemover();
String actual = remover.stripDuplicates(input);
assertEquals(expected, actual);
}
}
如果你喂线为LinkedHashSet
,它忽略重复的,因为它是一个集合,但保留顺序,因为它是链接的。如果你只是想知道你是否看过以前的行,在继续时将它们放入一个简单的Set
,并忽略Set已经包含/包含的行。
这里我用一个HashSet存储看出线
Scanner scan;//input
Set<String> lines = new HashSet<String>();
StringBuilder strb = new StringBuilder();
while(scan.hasNextLine()){
String line = scan.nextLine();
if(lines.add(line)) strb.append(line);
}
但是我们可以确保输入行和输出行的顺序与散列保持一致吗? – 2011-05-09 01:50:59
我也将它们添加到一个字符串生成器中,当你遍历整个文本时,你抛弃了这个集合并保留'strb.toString()' – 2011-05-09 01:53:48
当你添加到一个集合时,你不需要检查如果它已经在那里。另外,HashSets不保证顺序。 – Kal 2011-05-09 01:55:31
这里是另一种解决方案。让我们来使用UNIX!
cat MyFile.java | uniq > MyFile.java
编辑:哦等等,我重新读了这个话题。这是一个合法的解决方案,因为我设法成为语言不可知论者?
我想你可以在这里使用类似的解决方案:http://stackoverflow.com/questions/1088113/is-there-a-java-library-of-unix-functions。不过,如果你在UNIX系统上,我会尝试为脚本编写钩子。 – Mike 2011-05-09 02:13:43
它可以轻松地从使用新的Java API流文本或文件中的重复行。 Stream支持不同的聚合特性,如排序,区分和使用不同的Java现有数据结构及其方法。下面的例子可以用它来删除重复或使用流API
package removeword;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Stream;
import static java.nio.file.StandardOpenOption.*;
import static java.util.stream.Collectors.joining;
public class Java8UniqueWords {
public static void main(String[] args) throws IOException {
Path sourcePath = Paths.get("C:/Users/source.txt");
Path changedPath = Paths.get("C:/Users/removedDouplicate_file.txt");
try (final Stream<String> lines = Files.lines(sourcePath)
// .map(line -> line.toLowerCase()) /*optional to use existing string methods*/
.distinct()
// .sorted()) /*aggregrate function to sort disctincted line*/
{
final String uniqueWords = lines.collect(joining("\n"));
System.out.println("Final Output:" + uniqueWords);
Files.write(changedPath , uniqueWords.getBytes(),WRITE, TRUNCATE_EXISTING);
}
}
}
- 1. 批处理从文本文件中删除重复的行
- 2. 如何从文本文件中删除重复的行
- 3. 在Java中,从文件列表中删除重复的文件
- 4. 删除重复文本
- 5. 从复制文本中删除文本
- 6. 删除列中的重复文本
- 7. 删除Textarea中的重复文本
- 8. SQL - 从表中删除重复的行
- 9. 从表中删除重复的行
- 10. 使用Informatica从表中重复删除
- 11. 使用NSSet从NSMutableArray中删除重复
- 12. 删除CSV文件中的重复行
- 13. 删除在文本文件中的非重复的行
- 14. 从C#中的文本文件中删除带有时间戳的重复行
- 15. 删除文本文件中的重复行?
- 16. 使用powershell脚本删除excel(.xls)中的重复行
- 17. 如何从文件中删除重复的文本?
- 18. 从文本文件中删除重复和周围的线
- 19. 使用UNION删除重复的行
- 20. mysql从表中删除重复行
- 21. 从DataTable中删除重复行
- 22. 使用rowid从oracle数据库中删除重复的行?
- 23. 从重复中删除行后添加重复行
- 24. 从Java中的大文本文件中删除行
- 25. 如何从java中的文本文件中删除空行
- 26. 从Java中的文本文件中删除特定行
- 27. 在使用java的文本文件中删除一行文本行
- 28. 删除重复行
- 29. 删除重复行
- 30. 使用C删除文件中的所有重复行C
嗯排序在文件中的内容,让我。不知道。 – Mike 2011-05-09 02:03:30