refactor(tool): 优化文件处理逻辑和错误处理
This commit is contained in:
@@ -41,7 +41,7 @@ func main() {
|
||||
files, err := getTxtFiles("./")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
continue
|
||||
}
|
||||
if files != nil {
|
||||
start := time.Now()
|
||||
@@ -49,23 +49,23 @@ func main() {
|
||||
fileLines := make(map[string]int)
|
||||
fmt.Println("正在统计", len(files), "个文件行数")
|
||||
for _, filePath := range files {
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
fmt.Println("打开文件失败:", err)
|
||||
}
|
||||
defer file.Close()
|
||||
// 使用 bufio.Scanner 逐行读取
|
||||
scanner := bufio.NewScanner(file)
|
||||
lineCount := 0
|
||||
for scanner.Scan() {
|
||||
lineCount++
|
||||
}
|
||||
file.Close()
|
||||
if lineCount == 0 {
|
||||
return
|
||||
continue
|
||||
}
|
||||
fileLines[filepath.Base(filePath)] = lineCount
|
||||
fmt.Println(filepath.Base(filePath), "文件行数:", lineCount)
|
||||
|
||||
}
|
||||
|
||||
var tasks []Task
|
||||
|
||||
Reference in New Issue
Block a user