forked from rubyhan1314/Golang-100-Days
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b391d6c
commit 50f9128
Showing
28 changed files
with
2,701 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
abcdefghij |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
/* | ||
FileInfo:文件信息 | ||
interface | ||
Name(),文件名 | ||
Size(),文件大小,字节为单位 | ||
IsDir(),是否是目录 | ||
ModTime(),修改时间 | ||
Mode(),权限 | ||
*/ | ||
fileInfo,err := os.Stat("/Users/ruby/Documents/pro/a/aa.txt") | ||
if err != nil{ | ||
fmt.Println("err :",err) | ||
return | ||
} | ||
fmt.Printf("%T\n",fileInfo) | ||
//文件名 | ||
fmt.Println(fileInfo.Name()) | ||
//文件大小 | ||
fmt.Println(fileInfo.Size()) | ||
//是否是目录 | ||
fmt.Println(fileInfo.IsDir()) //IsDirectory | ||
//修改时间 | ||
fmt.Println(fileInfo.ModTime()) | ||
//权限 | ||
fmt.Println(fileInfo.Mode()) //-rw-r--r-- | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"path" | ||
"os" | ||
) | ||
|
||
func main() { | ||
/* | ||
文件操作: | ||
1.路径: | ||
相对路径:relative | ||
ab.txt | ||
相对于当前工程 | ||
绝对路径:absolute | ||
/Users/ruby/Documents/pro/a/aa.txt | ||
.当前目录 | ||
..上一层 | ||
2.创建文件夹,如果文件夹存在,创建失败 | ||
os.MkDir(),创建一层 | ||
os.MkDirAll(),可以创建多层 | ||
3.创建文件,Create采用模式0666(任何人都可读写,不可执行)创建一个名为name的文件,如果文件已存在会截断它(为空文件) | ||
os.Create(),创建文件 | ||
4.打开文件:让当前的程序,和指定的文件之间建立一个连接 | ||
os.Open(filename) | ||
os.OpenFile(filename,mode,perm) | ||
5.关闭文件:程序和文件之间的链接断开。 | ||
file.Close() | ||
5.删除文件或目录:慎用,慎用,再慎用 | ||
os.Remove(),删除文件和空目录 | ||
os.RemoveAll(),删除所有 | ||
*/ | ||
//1.路径 | ||
fileName1:="/Users/ruby/Documents/pro/a/aa.txt" | ||
fileName2:="bb.txt" | ||
fmt.Println(filepath.IsAbs(fileName1)) //true | ||
fmt.Println(filepath.IsAbs(fileName2)) //false | ||
fmt.Println(filepath.Abs(fileName1)) | ||
fmt.Println(filepath.Abs(fileName2)) // /Users/ruby/go/src/l_file/bb.txt | ||
|
||
fmt.Println("获取父目录:",path.Join(fileName1,"..")) | ||
|
||
//2.创建目录 | ||
//err := os.Mkdir("/Users/ruby/Documents/pro/a/bb",os.ModePerm) | ||
//if err != nil{ | ||
// fmt.Println("err:",err) | ||
// return | ||
//} | ||
//fmt.Println("文件夹创建成功。。") | ||
//err :=os.MkdirAll("/Users/ruby/Documents/pro/a/cc/dd/ee",os.ModePerm) | ||
//if err != nil{ | ||
// fmt.Println("err:",err) | ||
// return | ||
//} | ||
//fmt.Println("多层文件夹创建成功") | ||
|
||
//3.创建文件:Create采用模式0666(任何人都可读写,不可执行)创建一个名为name的文件,如果文件已存在会截断它(为空文件) | ||
//file1,err :=os.Create("/Users/ruby/Documents/pro/a/ab.txt") | ||
//if err != nil{ | ||
// fmt.Println("err:",err) | ||
// return | ||
//} | ||
//fmt.Println(file1) | ||
|
||
//file2,err := os.Create(fileName2)//创建相对路径的文件,是以当前工程为参照的 | ||
//if err != nil{ | ||
// fmt.Println("err :",err) | ||
// return | ||
//} | ||
//fmt.Println(file2) | ||
|
||
//4.打开文件: | ||
//file3 ,err := os.Open(fileName1) //只读的 | ||
//if err != nil{ | ||
// fmt.Println("err:",err) | ||
// return | ||
//} | ||
//fmt.Println(file3) | ||
/* | ||
第一个参数:文件名称 | ||
第二个参数:文件的打开方式 | ||
const ( | ||
// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified. | ||
O_RDONLY int = syscall.O_RDONLY // open the file read-only. | ||
O_WRONLY int = syscall.O_WRONLY // open the file write-only. | ||
O_RDWR int = syscall.O_RDWR // open the file read-write. | ||
// The remaining values may be or'ed in to control behavior. | ||
O_APPEND int = syscall.O_APPEND // append data to the file when writing. | ||
O_CREATE int = syscall.O_CREAT // create a new file if none exists. | ||
O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist. | ||
O_SYNC int = syscall.O_SYNC // open for synchronous I/O. | ||
O_TRUNC int = syscall.O_TRUNC // truncate regular writable file when opened. | ||
) | ||
第三个参数:文件的权限:文件不存在创建文件,需要指定权限 | ||
*/ | ||
//file4,err := os.OpenFile(fileName1,os.O_RDONLY|os.O_WRONLY,os.ModePerm) | ||
//if err != nil{ | ||
// fmt.Println("err:",err) | ||
// return | ||
//} | ||
//fmt.Println(file4) | ||
|
||
//5关闭文件, | ||
//file4.Close() | ||
|
||
//6.删除文件或文件夹: | ||
//删除文件 | ||
//err := os.Remove("/Users/ruby/Documents/pro/a/aa.txt") | ||
//if err != nil{ | ||
// fmt.Println("err:",err) | ||
// return | ||
//} | ||
//fmt.Println("删除文件成功。。") | ||
//删除目录 | ||
err := os.RemoveAll("/Users/ruby/Documents/pro/a/cc") | ||
if err != nil{ | ||
fmt.Println("err:",err) | ||
return | ||
} | ||
fmt.Println("删除目录成功。。") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func main() { | ||
/* | ||
读取数据: | ||
Reader接口: | ||
Read(p []byte)(n int, error) | ||
*/ | ||
//读取本地aa.txt文件中的数据 | ||
//step1:打开文件 | ||
fileName := "/Users/ruby/Documents/pro/a/aa.txt" | ||
file,err := os.Open(fileName) | ||
if err != nil{ | ||
fmt.Println("err:",err) | ||
return | ||
} | ||
//step3:关闭文件 | ||
defer file.Close() | ||
|
||
//step2:读取数据 | ||
bs := make([]byte,4,4) | ||
/* | ||
//第一次读取 | ||
n,err :=file.Read(bs) | ||
fmt.Println(err) //<nil> | ||
fmt.Println(n) //4 | ||
fmt.Println(bs) //[97 98 99 100] | ||
fmt.Println(string(bs)) //abcd | ||
//第二次读取 | ||
n,err = file.Read(bs) | ||
fmt.Println(err)//<nil> | ||
fmt.Println(n)//4 | ||
fmt.Println(bs) //[101 102 103 104] | ||
fmt.Println(string(bs)) //efgh | ||
//第三次读取 | ||
n,err = file.Read(bs) | ||
fmt.Println(err) //<nil> | ||
fmt.Println(n) //2 | ||
fmt.Println(bs) //[105 106 103 104] | ||
fmt.Println(string(bs)) //ijgh | ||
//第四次读取 | ||
n,err = file.Read(bs) | ||
fmt.Println(err) //EOF | ||
fmt.Println(n) //0 | ||
*/ | ||
n := -1 | ||
for{ | ||
n,err = file.Read(bs) | ||
if n == 0 || err == io.EOF{ | ||
fmt.Println("读取到了文件的末尾,结束读取操作。。") | ||
break | ||
} | ||
fmt.Println(string(bs[:n])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
func main() { | ||
/* | ||
写出数据: | ||
*/ | ||
|
||
fileName := "/Users/ruby/Documents/pro/a/ab.txt" | ||
//step1:打开文件 | ||
//step2:写出数据 | ||
//step3:关闭文件 | ||
//file,err := os.Open(fileName) | ||
file,err := os.OpenFile(fileName,os.O_CREATE|os.O_WRONLY|os.O_APPEND,os.ModePerm) | ||
if err != nil{ | ||
fmt.Println(err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
//写出数据 | ||
//bs :=[]byte{65,66,67,68,69,70}//A,B,C,D,E,F | ||
bs :=[] byte{97,98,99,100} //a,b,c,d | ||
//n,err := file.Write(bs) | ||
n,err := file.Write(bs[:2]) | ||
fmt.Println(n) | ||
HandleErr(err) | ||
file.WriteString("\n") | ||
|
||
//直接写出字符串 | ||
n,err = file.WriteString("HelloWorld") | ||
fmt.Println(n) | ||
HandleErr(err) | ||
|
||
file.WriteString("\n") | ||
n,err =file.Write([]byte("today")) | ||
fmt.Println(n) | ||
HandleErr(err) | ||
|
||
} | ||
|
||
func HandleErr(err error){ | ||
if err != nil{ | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"io" | ||
"fmt" | ||
"io/ioutil" | ||
) | ||
|
||
func main() { | ||
/* | ||
拷贝文件: | ||
*/ | ||
srcFile := "/Users/ruby/Documents/pro/a/guliang.jpeg" | ||
destFile := "guliang3.jpeg" | ||
//total,err := CopyFile1(srcFile,destFile) | ||
//total,err := CopyFile2(srcFile,destFile) | ||
total,err := CopyFile3(srcFile,destFile) | ||
fmt.Println(total,err) | ||
} | ||
|
||
func CopyFile3(srcFile,destFile string)(int,error){ | ||
bs,err := ioutil.ReadFile(srcFile) | ||
if err != nil{ | ||
return 0,err | ||
} | ||
err = ioutil.WriteFile(destFile,bs,0777) | ||
if err != nil{ | ||
return 0,err | ||
} | ||
return len(bs),nil | ||
} | ||
|
||
func CopyFile2(srcFile,destFile string)(int64,error){ | ||
file1,err := os.Open(srcFile) | ||
if err != nil{ | ||
return 0,err | ||
} | ||
file2,err := os.OpenFile(destFile,os.O_WRONLY|os.O_CREATE,os.ModePerm) | ||
if err != nil{ | ||
return 0,err | ||
} | ||
defer file1.Close() | ||
defer file2.Close() | ||
return io.Copy(file2,file1) | ||
} | ||
|
||
|
||
//该函数:用于通过io操作实现文件的拷贝,返回值是拷贝的总数量(字节),错误 | ||
func CopyFile1(srcFile,destFile string)(int,error){ | ||
file1,err :=os.Open(srcFile) | ||
if err != nil{ | ||
return 0,err | ||
} | ||
file2,err := os.OpenFile(destFile,os.O_WRONLY|os.O_CREATE,os.ModePerm) | ||
if err != nil{ | ||
return 0,err | ||
} | ||
defer file1.Close() | ||
defer file2.Close() | ||
|
||
//读写 | ||
bs := make([]byte,1024,1024) | ||
n := -1 //读取的数据量 | ||
total := 0 | ||
for{ | ||
n,err = file1.Read(bs) | ||
if err == io.EOF || n==0{ | ||
fmt.Println("拷贝完毕。。") | ||
break | ||
}else if err != nil{ | ||
fmt.Println("报错了。。") | ||
return total,err | ||
} | ||
total += n | ||
file2.Write(bs[:n]) | ||
} | ||
return total,nil | ||
} | ||
|
Oops, something went wrong.