2016-08-02 149 views
0

如何爲追加文件指定Scala-IO library的選項。該manual給出了以下說明:用Scala-IO追加文件

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

回答