• go语言文件操作


    标准流的操作
    从标准输入中查找重复的

    // 从标准输入中查找重复的行
    func main() {
    	counts := make(map[string]int, 0)
    	scanner := bufio.NewScanner(os.Stdin) 
    	for scanner.Scan() {
    		counts[scanner.Text()]++
    	}
    
    	for key, value := range counts {
    		if value > 1 {
    			fmt.Println("输出======",key)
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    命令行输入和输出(需要等一定超时时间才能输出,可以按control+D发送中断信号可直接输出并结束)

    123
    123
    3
    1
    1
    输出====== 123
    输出====== 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    文件操作

    下面文件名scanner.go

    func main() {
    	cmdArgs := os.Args[1:]
    	files := make([]*os.File, 0) 
    	for _, path := range cmdArgs {
    		// 打开文件,以读写方式打开,如果没有则创建
    		file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0777)
    		if err != nil {
    			fmt.Println("打开文件失败:", path,"失败原因:", err)
    			return
    		}
    		// 收集文件指针
    		files = append(files, file)
    	}
    	// 文件名-总行数映射,文件名-去重行数映射,文件名-去重行映射
    	allLineCountMap, distinctLineCountMap, distinctLineMap := FileLineCount(files)
    	// 遍历打印结果
    	for _, file := range files {
    		fmt.Println("文件名:", file.Name(), " 行数:", allLineCountMap[file.Name()], " 去重行数:",distinctLineCountMap[file.Name()], "去重行:")
    		for _, line :=range distinctLineMap[file.Name()] {
    			fmt.Println(line)
    		}
    	}
    }
    
    // 文件统计,总行数,去重行数,去重行
    func FileLineCount(files []*os.File) (allLineCountMap map[string]int, distinctLineCountMap map[string]int, distinctLineMap map[string][]string) {
    	// 固定缓冲区大小,用来读文件
    	bytes := make([]byte, 512, 512)
    	// 文件结尾会返回io.EOF固定错误,用来判断文件遍历完成
    	var err error
    	// 文件名对应字节数组映射
    	fileNameToByteMap := make(map[string][]byte, 0)
    	// 遍历文件数组
    	for _, file := range files {
    		n := 0
    		err = nil
    		fileByte := make([]byte, 0, 1024)
    		for err != io.EOF {
    			// n是读取到了多少个字节,err是本次读取的错误信息
    			n, err = file.Read(bytes)
    			if n > 0 {
    				fileByte = append(fileByte, bytes[0:n]...)
    				// 清空bytes数组
    				for i := 0; i < n; i++ {
    					bytes[i] = 0
    				}
    			} else {
    				fileNameToByteMap[file.Name()] = fileByte
    			}
    
    			if err == io.EOF {
    				// 读文件结束,跳出内层循环
    				break
    			}
    		}
    	}
    
    	allLineCountMap = make(map[string]int, 0)
    	distinctLineCountMap = make(map[string]int, 0)
    	distinctLineMap = make(map[string][]string, 0)
    	// 读文件字节数组,统计行数
    	for fileName, fileBytes := range fileNameToByteMap {
    		// 字节数组按照\n分割
    		lines := strings.Split(string(fileBytes), "\n")
    		// 统计总行数
    		allLineCountMap[fileName] = len(lines)
    
    		// 统计去重行数
    		distinctMap := make(map[string]int, 0)
    		for _, line := range lines {
    			distinctMap[line]++
    		}
    
    		distinctLines := make([]string, 0, len(distinctLineMap))
    		for key := range distinctMap {
    			distinctLines = append(distinctLines, key)
    		}
    		distinctLineCountMap[fileName] = len(distinctMap)
    		distinctLineMap[fileName] = distinctLines
    	}
    	return
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    使用

    1. 创建测试文件text1.txt
    流程1
    流程1
    
    a
    a
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 创建测试文件text2.txt
    123
    123
    123
    123
    123
    123
    123
    123
    123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 执行go build scanner.go,生成scanner可执行文件
    2. 执行./scanner ./text1.txt ./text2.txt(目录需要改成自己的)
    3. 执行结果
    文件名: ./text1.txt  行数: 5  去重行数: 3 去重行:
    流程1
    
    a
    文件名: ./text2.txt  行数: 9  去重行数: 1 去重行:
    123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    35. 搜索插入位置 --力扣 --JAVA
    【深度学习】实验5布置:滴滴出行-交通场景目标检测
    流程系统的设计与实现
    斯坦福NLP课程 | 第11讲 - NLP中的卷积神经网络
    Shopee买家通系统怎么用的?
    JDK1.8Stream根据条件过滤出两个List集合中不一样的数据
    WEB漏洞-文件操作之文件包含漏洞
    时光邮局|来写一封未来的信试试吧!一个我的新项目,Java+Vue
    PDF 拆分/合并
    JDBC-day06(数据库连接池)
  • 原文地址:https://blog.csdn.net/weixin_43093878/article/details/136664839