怎样快速生成一个指定大小的文件

zhaohao · · 4120 次点击
<a href="/user/aboc" title="@aboc">@aboc</a> **学习了**
#3
更多评论
```golang package main import ( &#34;os&#34; &#34;math&#34; &#34;strings&#34; &#34;fmt&#34; ) func createFile(size float64,filename string){ size = math.Ceil(size) fmt.Println(size) f,err := os.OpenFile(filename,os.O_CREATE|os.O_APPEND|os.O_RDWR,os.ModeAppend|os.ModeSetuid) if err != nil{ panic(err) } defer f.Close() count := math.Ceil(float64(size) / 1000) count_64 := int64(int(count)) var i int64 var length int for i = 0;i&lt;count_64;i++{ if i == (count_64-1){ length = int(int64(size) - (i) *1000) } else { length = 1000 } f.WriteAt([]byte(strings.Repeat(&#34;A&#34;,length)),i*1000) } } func main(){ createFile(5.1*1024*1024,&#34;data/file3.txt&#34;) fmt.Println(&#34;OK&#34;) } ``` 这样的算吗?
#1
```golang package main import ( &#34;os&#34; &#34;math&#34; &#34;strings&#34; &#34;fmt&#34; ) func createFile(size float64,filename string){ size = math.Ceil(size) fmt.Println(size) f,err := os.OpenFile(filename,os.O_CREATE|os.O_APPEND|os.O_RDWR,os.ModeAppend|os.ModeSetuid) if err != nil{ panic(err) } defer f.Close() count := math.Ceil(float64(size) / 1000) count_64 := int64(int(count)) var i int64 var length int for i = 0;i&lt;count_64;i++{ if i == (count_64-1){ length = int(int64(size) - (i) *1000) } else { length = 1000 } f.WriteAt([]byte(strings.Repeat(&#34;A&#34;,length)),i*1000) } } func main(){ createFile(5.1*1024*1024,&#34;data/file3.txt&#34;) fmt.Println(&#34;OK&#34;) } ```
#2