删除文本文件中重复行的Java程序

先决条件: 字符打印流 , 缓冲区读取

null

给定一个文件输入。txt。我们的任务是从中删除重复的行,并将输出保存在文件中,例如output。txt

朴素算法:

1. Create PrintWriter object for output.txt
2. Open BufferedReader for input.txt
3. Run a loop for each line of input.txt
   3.1 flag = false
   3.2 Open BufferedReader for output.txt
   3.3 Run a loop for each line of output.txt
      ->  If  line of output.txt is equal to current line of input.txt 
            -> flag = true
            -> break loop

4. Check flag, if false
     -> write current line of input.txt to output.txt
     -> Flush PrintWriter stream

5. Close resources.

要成功运行以下程序输入。txt必须存在于同一文件夹中,或提供其完整路径。

// Java program to remove
// duplicates from input.txt and
// save output to output.txt
import java.io.*;
public class FileOperation
{
public static void main(String[] args) throws IOException
{
// PrintWriter object for output.txt
PrintWriter pw = new PrintWriter( "output.txt" );
// BufferedReader object for input.txt
BufferedReader br1 = new BufferedReader( new FileReader( "input.txt" ));
String line1 = br1.readLine();
// loop for each line of input.txt
while (line1 != null )
{
boolean flag = false ;
// BufferedReader object for output.txt
BufferedReader br2 = new BufferedReader( new FileReader( "output.txt" ));
String line2 = br2.readLine();
// loop for each line of output.txt
while (line2 != null )
{
if (line1.equals(line2))
{
flag = true ;
break ;
}
line2 = br2.readLine();
}
// if flag = false
// write line of input.txt to output.txt
if (!flag){
pw.println(line1);
// flushing is important here
pw.flush();
}
line1 = br1.readLine();
}
// closing resources
br1.close();
pw.close();
System.out.println( "File operation performed successfully" );
}
}


输出:

File operation performed successfully

注: 如果输出。txt存在于cwd(当前工作目录)中,然后它将被上述程序覆盖,否则将创建新文件。

A. 更好的解决方案 就是使用 哈希集 存储每行输入。txt。As set会忽略重复的值,所以在存储行时,请检查它是否已经存在于哈希集中。将其写入输出。仅当hashset中不存在时才使用txt。

要成功运行以下程序输入。txt必须存在于同一文件夹中,或为其提供完整路径。

// Efficient Java program to remove
// duplicates from input.txt and
// save output to output.txt
import java.io.*;
import java.util.HashSet;
public class FileOperation
{
public static void main(String[] args) throws IOException
{
// PrintWriter object for output.txt
PrintWriter pw = new PrintWriter( "output.txt" );
// BufferedReader object for input.txt
BufferedReader br = new BufferedReader( new FileReader( "input.txt" ));
String line = br.readLine();
// set store unique values
HashSet<String> hs = new HashSet<String>();
// loop for each line of input.txt
while (line != null )
{
// write only if not
// present in hashset
if (hs.add(line))
pw.println(line);
line = br.readLine();
}
pw.flush();
// closing resources
br.close();
pw.close();
System.out.println( "File operation performed successfully" );
}
}


输出:

File operation performed successfully

注: 如果输出。txt存在于cwd(当前工作目录)中,然后它将被上述程序覆盖,否则将创建新文件。

本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享