单个txt文件去除重复值

Posted Limpie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单个txt文件去除重复值相关的知识,希望对你有一定的参考价值。

首先,txt文件的值要多行显示,即一个值占一行。
然后上代码:

public static void distinct() 
	String pathname = "haveDuplicate.txt";
	Map<String, String> map = new HashMap<String, String>();
	try (FileReader reader = new FileReader(pathname); 
			BufferedReader br = new BufferedReader(reader);) 
		String line;
		
		// 每次读取一行数据,直接写入map中的Key
        while ((line = br.readLine()) != null) 
            if(line.isEmpty()) 
        		continue;
        	
        	line=line.trim();
            map.put(line, "Limpie");
        
        br.close();
           
		// 下面是写入文件
		File writeName = new File("eliminationDuplicate.txt");
		writeName.createNewFile();
		List<String> diff = new ArrayList<String>();
		try (FileWriter writer = new FileWriter(writeName); BufferedWriter out = new BufferedWriter(writer)) 
			//遍历map,然后写入文件
			for (Map.Entry<String, String> entry : map.entrySet()) 
				diff.add(entry.getKey());
			
			out.write(String.join("\\n", diff) + "");
			out.flush(); // 把缓存区内容压入文件
			out.close();
		
	 catch (IOException e) 
		System.out.println("读取剔重文件内容出错");
		e.printStackTrace();
		

测试的文件:

结果:

以上是关于单个txt文件去除重复值的主要内容,如果未能解决你的问题,请参考以下文章

Python拼接两个TXT文件(遍历文件夹,去除重复记录)

[Python]pyhon去除txt文件重复行 python 2020.2.10

利用Python将txt文件批量去除重复行内容

sql查询去除重复值语句

使用批处理脚本从多个相同的 .txt 文件中复制某个值 [重复]

Shell命令学习 之,sort,unique 文件简单排序去除重复记录