使用Scala-IO追加文件

时间:2016-08-02 11:26:02

标签: scala io

如何为Scala-IO library指定附加文件的选项。 manual提供了以下说明:

import scalax.io.Resource._
import java.io.File
import scalax.io.{Seekable,Codec}
// see codec example for why codec is required
implicit val codec = Codec.UTF8

val someFile: Seekable = fromFile("someFile")

// write bytes
// By default the file write will replace
// an existing file with the new data
someFile.write (Array (1,2,3) map ( _.toByte))

// another option for write is openOptions which allows the caller
// to specify in detail how the write should take place
// the openOptions parameter takes a collections of OpenOptions objects
// which are filesystem specific in general but the standard options
// are defined in the OpenOption object
// in addition to the definition common collections are also defined
// WriteAppend for example is a List(Create, Append, Write)
someFile.write (List (1,2,3) map (_.toByte))

// write a string to the file
someFile.write("Hello my dear file")

// with all options (these are the default options explicitely declared)
someFile.write("Hello my dear file")(codec = Codec.UTF8)

// Convert several strings to the file
// same options fromString as for write
someFile.writeStrings( "It costs" :: "one" :: "dollar" :: Nil)

// Now all options
someFile.writeStrings("It costs" :: "one" :: "dollar" :: Nil,
            separator="||\n||")(codec = Codec.UTF8)

如何使用openOptions

1 个答案:

答案 0 :(得分:2)

根据http://jesseeichar.github.io/scala-io-doc/0.4.3/api/index.html#scalax.io.Seekable

  

scalax.io.Output中的方法始终是完全破坏性的。 IE写入将替换文件中的所有数据,插入,补丁或追加是你的朋友,如果这不是你想要的行为

即。使用append,而不是writeSeekable.write没有openOptions参数。所以手册可能已经过时了。