You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package FileUtil
import (
"bufio"
"fmt"
"os"
)
// 判断所给路径文件/文件夹是否存在
func Exists ( path string ) bool {
_ , err := os . Stat ( path ) //os.Stat获取文件信息
if err != nil {
if os . IsExist ( err ) {
return true
}
return false
}
return true
}
/**
功能:按行读取文件
作者:黄海
时间: 2020-07-08
*/
func ReadLines ( path string ) ( [ ] string , error ) {
file , err := os . Open ( path )
if err != nil {
return nil , err
}
defer file . Close ( )
var lines [ ] string
scanner := bufio . NewScanner ( file )
for scanner . Scan ( ) {
lines = append ( lines , scanner . Text ( ) )
}
return lines , scanner . Err ( )
}
/**
功能:按行写入文件
作者:黄海
时间: 2020-07-08
*/
func WriteLines ( lines [ ] string , path string ) error {
file , err := os . Create ( path )
if err != nil {
return err
}
defer file . Close ( )
w := bufio . NewWriter ( file )
for _ , line := range lines {
fmt . Fprintln ( w , line )
}
return w . Flush ( )
}