zoukankan      html  css  js  c++  java
  • 文本文件按行去重,有序输出

    针对有些文本数据重复的情况,需要将数据去重,考虑到LinkedHashMap是有序的,可以保证文本顺序不变,所以采用此集合。

    去重前:
    image

    去重后:
    image

    代码如下:

    import java.io.*;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * @program: receiveDemo
     * @description: 文件去重
     * @author: huang wei
     * @create: 2021-04-09 09:57
     */
    public class FileTest {
    	public static void main(String[] args) {
    		String oldFilePath = "H:\upload\test\";
    		String newFilePath = "H:\upload\test2\";
    
    		// 遍历文件夹
    		File directory = new File(oldFilePath);
    		if (directory.isDirectory()) {
    			File[] files = directory.listFiles();
    			for (File file : files) {
    				if (!file.isDirectory()) {
    					String result = removeDuplicate(file.getAbsolutePath());
    					writeData(newFilePath + file.getName(), result);
    				}
    			}
    		}else {
    			String result = removeDuplicate(directory.getAbsolutePath());
    			writeData(newFilePath + directory.getName(), result);
    		}
    	}
    
    	/**
    	 * @throws Exception
    	 * @Description: 文本按行去重
    	 * @Param:
    	 * @return:
    	 * @author: hw
    	 * @date: 2021/4/13 11:35
    	 */
    	public static String removeDuplicate(String filePath) {
    		String str;
    		StringBuffer stringBuffer = new StringBuffer();
    		Map<String, String> map = new LinkedHashMap<>();
    
    		try (FileReader reader = new FileReader(filePath);
    			 BufferedReader br = new BufferedReader(reader)) {
    			while ((str = br.readLine()) != null) {
    				map.put(str, "");
    			}
    
    			Set<String> set = map.keySet();
    			Iterator<String> iterator = set.iterator();
    			while (iterator.hasNext()) {
    				String key = iterator.next();
    				stringBuffer.append(key).append("
    ");
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return stringBuffer.toString();
    	}
    
    	/**
    	 * @throws Exception
    	 * @Description: 文件写入
    	 * @Param:
    	 * @return:
    	 * @author: hw
    	 * @date: 2021/4/13 11:34
    	 */
    	public static void writeData(String filePath, String content) {
    		RandomAccessFile randomFile = null;
    		try {
    			// 打开一个随机访问文件流,按读写方式
    			randomFile = new RandomAccessFile(filePath, "rw");
    			// 文件长度,字节数
    			long fileLength = randomFile.length();
    			// 将写文件指针移到文件尾。
    			randomFile.seek(fileLength);
    			// 将数据转成byte防止中文乱码
    			byte buffer[] = new byte[1024];
    			buffer = content.getBytes();
    			randomFile.write(buffer);
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (randomFile != null) {
    				try {
    					randomFile.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    }
    ========================================================================================== 我希望每一篇文章的背后,都能看到自己对于技术、对于生活的态度。 我相信乔布斯说的,只有那些疯狂到认为自己可以改变世界的人才能真正地改变世界。面对压力,我可以挑灯夜战、不眠不休;面对困难,我愿意迎难而上、永不退缩。 其实我想说的是,我只是一个程序员,这就是我现在纯粹人生的全部。 ==========================================================================================
  • 相关阅读:
    天平称重【递归解法】
    天平称重【三进制巧解】
    天平称重【暴力解】
    奇怪的捐赠
    日期问题
    承压计算
    python学习(3)关于交互输入及字符串拼接
    python学习(2)关于字符编码
    python学习(1)python的基本概念
    Spring整合kafka消费者和生产者&redis的步骤
  • 原文地址:https://www.cnblogs.com/weihuang6620/p/14653701.html
Copyright © 2011-2022 走看看