如何在Scala中写入文件? [英] How to write to a file in Scala?

查看:338
本文介绍了如何在Scala中写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于阅读,有一个有用的抽象 Source 。如何写行到文本文件?

解决方案

编辑(2011年9月):自Eduardo科斯塔询问Scala2.9,由于 Rick-777 评论说 scalax.IO提交历史记录很漂亮自2009年年中以来,这一切都不存在...

Scala-IO 发生了变化:请参阅 GitHub库时,从赫塞·艾查尔(也关于):


< Scala IO伞项目包括的几个子项目的不同方面和扩展的IO。

Scala IO有两个主要组件:


  • - Core主要处理从任意源和汇的读写数据。角落的石头特征是 Input , Output Seekable which提供核心API。

    其他重要类是 Resource ReadChars WriteChars

  • 文件 - 文件是文件称为 Path )基于Java 7 NIO文件系统和SBT PathFinder API组合的API。
    Path FileSystem 是Scala IO File API的主要入口点。



  import scalax.io._ 

val输出:输出= Resource.fromFile(someFile)

//注意:每次写入都会打开一个新的文件连接,
//每次写入都会在文件开始时执行
//所以在这种情况下最后一次写入将是文件的内容。
// See Seeable for append and patching files
//另请参阅openOutput用于执行多次写入操作

output.writeIntsAsBytes(1,2,3)
output.write(hello)(Codec.UTF8)
output.writeStrings(List(hello,world),)(Codec.UTF8)



原始答案(2011年1月),与scala-io的旧地方: p>

如果你不想等待Scala2.9,你可以使用 scala-incubator / scala-io 库。
(如

请参阅样品

  {//几个写数据的例子
import scalax.io。{
FileOps,Path,Codec,OpenOption}
//编解码器必须定义为ops方法的参数,或者作为隐式
implicit val codec = scalax.io.Codec.UTF8


val file:FileOps = Path(file)

//写入字节
//默认情况下,文件写入将替换
//一个现有文件与新的数据
file.write(Array(1,2,3)map(_.toByte))

//另一个用于写入的选项是openOptions,它允许调用者
//详细指定写入应该如何进行
// openOptions参数接受一组OpenOptions对象
//通常是特定于文件系统的,但标准选项
//在OpenOption对象中定义
//除定义常见集合外,还定义了
// WriteAppend for例子是一个List(Create,Append,Write)
file.write( List(1,2,3)map(_.toByte))

//写一个字符串到文件
file.write(Hello my dear file)

//带有所有选项(这些是明确声明的默认选项)
file.write(Hello my dear file)(codec = Codec.UTF8)

//将几个字符串转换为文件
//相同的选项适用于写入
file.writeStrings(It costs::one::dollar:: Nil)

//现在所有选项
file.writeStrings(It costs::one::dollar:: Nil,
separator =|| \\\
||)( codec = Codec.UTF8)
}


For reading, there is the useful abstraction Source. How can I write lines to a text file?

解决方案

Edit (September 2011): since Eduardo Costa asks about Scala2.9, and since Rick-777 comments that scalax.IO commit history is pretty much non-existent since mid-2009...

Scala-IO has changed place: see its GitHub repo, from Jesse Eichar (also on SO):

The Scala IO umbrella project consists of a few sub projects for different aspects and extensions of IO.
There are two main components of Scala IO:

  • Core - Core primarily deals with Reading and writing data to and from arbitrary sources and sinks. The corner stone traits are Input, Output and Seekable which provide the core API.
    Other classes of importance are Resource, ReadChars and WriteChars.
  • File - File is a File (called Path) API that is based on a combination of Java 7 NIO filesystem and SBT PathFinder APIs.
    Path and FileSystem are the main entry points into the Scala IO File API.

import scalax.io._

val output:Output = Resource.fromFile("someFile")

// Note: each write will open a new connection to file and 
//       each write is executed at the begining of the file,
//       so in this case the last write will be the contents of the file.
// See Seekable for append and patching files
// Also See openOutput for performing several writes with a single connection

output.writeIntsAsBytes(1,2,3)
output.write("hello")(Codec.UTF8)
output.writeStrings(List("hello","world")," ")(Codec.UTF8)


Original answer (January 2011), with the old place for scala-io:

If you don't want to wait for Scala2.9, you can use the scala-incubator / scala-io library.
(as mentioned in "Why doesn't Scala Source close the underlying InputStream?")

See the samples

{ // several examples of writing data
    import scalax.io.{
      FileOps, Path, Codec, OpenOption}
    // the codec must be defined either as a parameter of ops methods or as an implicit
    implicit val codec = scalax.io.Codec.UTF8


    val file: FileOps = Path ("file")

    // write bytes
    // By default the file write will replace
    // an existing file with the new data
    file.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)
    file.write (List (1,2,3) map (_.toByte))

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

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

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

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

这篇关于如何在Scala中写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆