scalax.io._覆盖文件中数据的最佳方法 [英] scalax.io._ Best way to overwrite data in file

查看:31
本文介绍了scalax.io._覆盖文件中数据的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,对scalax.io._中的文件进行写操作的默认方式是追加".但是,与Resource.fromFile对象一起操作以重写诸如系统IO open(..,WRITE)调用之类的数据的最佳和最短方法是什么?我的想法是删除文件或使用seek/patching API.但对于这种简单的操作,他们会很冗长.

I have found that default way in write operations on file in scalax.io._ is "append". But what the best and shortest way to operate with Resource.fromFile object for ovewriting data like system IO open(..,WRITE) call ? The Ideas that i have are deleting file or using seeking/patching API . but they to verbose for such simple operation .

scala> def mk(s:String) = { val out: Output = Resource.fromFile("/tmp/data");out.write(s)}
mk: (s: String)Unit

scala> mk("XXXX")
scala> mk("XXXX")

bash# > ls -l /tmp/data

-rw-rw-r-- 1 root root 8 Jun  1 16:27 /tmp/data

推荐答案

使用普通的旧输出流:

import scalax.io.Resource
import scalax.io._
val out:Output = Resource.fromOutputStream(new java.io.FileOutputStream("/tmp/testFile"))


out write "howdy"
out write "bah"

请小心,因为每次写入都会重写文件.请参阅每日scala 如何解决此问题,以及有关更多示例

Be careful as file will be rewritten on each write. See daily scala how to overcome this, and for more examples

这篇关于scalax.io._覆盖文件中数据的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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