反向读取文件

w1281472252 · · 1187 次点击
<a href="/user/victorl" title="@victorl">@victorl</a> 好的 谢谢~! 我去看看怎么操作
#8
更多评论
请问这样做的需求是什么?可以试试:先读取文件,然后转为string后,反转一下?
#1
临时写了一个,测试了一下可以,如用于生产环境的话还需要严格测试。 ```go package file import ( &#34;bytes&#34; &#34;fmt&#34; &#34;io&#34; &#34;os&#34; ) var ( buffSize = 1 &lt;&lt; 20 ) // ReadLineFromEnd -- type ReadLineFromEnd struct { f *os.File fileSize int bwr *bytes.Buffer swapBuff []byte } // NewReadLineFromEnd -- func NewReadLineFromEnd(name string) (rd *ReadLineFromEnd, err error) { f, err := os.Open(name) if err != nil { return nil, err } info, err := f.Stat() if info.IsDir() { return nil, fmt.Errorf(&#34;not file&#34;) } fileSize := int(info.Size()) rd = &amp;ReadLineFromEnd{ f: f, fileSize: fileSize, bwr: bytes.NewBuffer([]byte{}), swapBuff: make([]byte, buffSize), } return rd, nil } // Read -- func (c *ReadLineFromEnd) Read(p []byte) (n int, err error) { err = c.buff() if err != nil { return n, err } return c.bwr.Read(p) } // ReadLine 结尾包含&#39;\n&#39; func (c *ReadLineFromEnd) ReadLine() (line []byte, err error) { err = c.buff() if err != nil { return nil, err } return c.bwr.ReadBytes(&#39;\n&#39;) } // Close -- func (c *ReadLineFromEnd) Close() (err error) { return c.f.Close() } func (c *ReadLineFromEnd) buff() (err error) { if c.fileSize == 0 { return nil } if c.bwr.Len() &gt;= buffSize { return nil } offset := 0 if c.fileSize &gt; buffSize { offset = c.fileSize - buffSize } _, err = c.f.Seek(int64(offset), 0) if err != nil { return err } n, err := c.f.Read(c.swapBuff) if err != nil &amp;&amp; err != io.EOF { return err } if n == 0 { return nil } for { m := bytes.LastIndex(c.swapBuff[:n], []byte{&#39;\n&#39;}) if m == -1 { break } if m &lt; n-1 { _, err = c.bwr.Write(c.swapBuff[m+1 : n]) if err != nil { return err } _, err = c.bwr.Write([]byte{&#39;\n&#39;}) if err != nil { return err } } n = m if n == 0 { break } } if n &gt; 0 { _, err := c.bwr.Write(c.swapBuff[:n]) if err != nil { return err } } c.fileSize = offset return nil } ``` 测试代码 ```go func TestNewReadLineFromEnd(t *testing.T) { rd, err := NewReadLineFromEnd(&#34;./123.log&#34;) if err != nil { t.Fatal(err) } defer rd.Close() for { data, err := rd.ReadLine() if err != nil { if err != io.EOF { t.Fatal(err) } break } fmt.Print(string(data)) } } ```
#2