对于在Scala中读取文件,有
Source.fromFile("file.txt").mkString
Run Code Online (Sandbox Code Playgroud)
是否有一种等效和简洁的方法来将字符串写入文件?
大多数语言支持这样的东西.我最喜欢的是Groovy:
def f = new File("file.txt")
// Read
def s = f.text
// Write
f.text = "file contents"
Run Code Online (Sandbox Code Playgroud)
我想将代码用于从单行到短代码的程序.必须使用自己的库在这里没有意义.我希望有一种现代语言能让我方便地写一些文件.
有类似的帖子,但他们没有回答我的确切问题或专注于较旧的Scala版本.
例如:
我在Web上看到了许多针对Scala的ARM(自动资源管理)示例.虽然大多数看起来很像彼此,但它似乎是一种写作的通道.不过,我确实看到了一个使用延续的非常酷的例子.
无论如何,很多代码都有这种或那种类型的缺陷,所以我认为在Stack Overflow上有一个引用是个好主意,我们可以在这里投票给出最正确和最合适的版本.
如何使用Scala Stream读取大型CSV文件(> 1 Gb)?你有代码示例吗?或者您是否会使用不同的方式来读取大型CSV文件而不先将其加载到内存中?
我已经定义了'使用'功能如下:
def using[A, B <: {def close(): Unit}] (closeable: B) (f: B => A): A =
try { f(closeable) } finally { closeable.close() }
Run Code Online (Sandbox Code Playgroud)
我可以这样使用它:
using(new PrintWriter("sample.txt")){ out =>
out.println("hellow world!")
}
Run Code Online (Sandbox Code Playgroud)
现在我很好奇如何定义'使用'函数来获取任意数量的参数,并且能够单独访问它们:
using(new BufferedReader(new FileReader("in.txt")), new PrintWriter("out.txt")){ (in, out) =>
out.println(in.readLIne)
}
Run Code Online (Sandbox Code Playgroud) 我想写一个类似于以下的方法
def appendFile(fileName: String, line: String) = {
}
Run Code Online (Sandbox Code Playgroud)
但我不确定如何充实实施.这里的另一个问题暗示了Scala 2.9功能,但我找不到更多细节.
有没有更好的方法来确保资源得到正确发布 - 更好的方法来编写以下代码?
val out: Option[FileOutputStream] = try {
Option(new FileOutputStream(path))
} catch {
case _ => None
}
if (out.isDefined) {
try {
Iterator.continually(in.read).takeWhile(-1 != _).foreach(out.get.write)
} catch {
case e => println(e.getMessage)
} finally {
in.close
out.get.flush()
out.get.close()
}
}
Run Code Online (Sandbox Code Playgroud) C#有using与IDisposable接口.Java的7+有相同的功能try和AutoCloseable接口.Scala允许您为此问题选择自己的实现.
scala-arm似乎是受欢迎的选择,并由Typesafe员工之一维护.但是,这种简单的行为似乎非常复杂.为了澄清,使用说明很简单,但了解所有代码在内部工作的方式相当复杂.
我刚刚编写了以下超级简单的ARM解决方案:
object SimpleARM {
def apply[T, Q](c: T {def close(): Unit})(f: (T) => Q): Q = {
try {
f(c)
} finally {
c.close()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Scala,Source.fromFile但是一旦读取了文件,我似乎无法找到一种很好的方法将它close传入底层InputStream.
这是我的代码,AssertionError因为无法删除文件而失败.
def main(args : Array[String]) : Unit = {
val myFile = new File("c:/tmp/doodah.txt")
var src = Source.fromFile(myFile)
src.getLines.foreach(l => print(l))
val deleted: Boolean = myFile.delete
assert (deleted , "File was not deleted - maybe the stream hasn't been closed in Source")
}
Run Code Online (Sandbox Code Playgroud)
Source有一个被调用的方法,reset但是这样做会重新创建文件中的源.
内部Source创建一个BufferedSource具有close方法的底层.但是,这不是从Source公开的.
我希望Source在读取文件内容后释放文件句柄,但似乎没有这样做.
到目前为止,我看到的最佳解决方法是基本上将其转换Source为a BufferedSource和call close.
try {
src.getLines.foreach(l => print(l))
}
finally src match { …Run Code Online (Sandbox Code Playgroud) Java 7引入了自动资源管理:
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
Run Code Online (Sandbox Code Playgroud)
这适用于任何实现的类java.lang.AutoClosable.
我知道有几个在Scala中进行自动资源管理的例子,包括Martin Odersky演示的一个例子.
是否有计划向Scala添加语言级资源管理,类似于Java try(...) { }?
在Scala应用程序中,我尝试使用java nio try-with-resource构造从文件中读取行.
Scala版本2.11.8
Java版本1.8
try(Stream<String> stream = Files.lines(Paths.get("somefile.txt"))){
stream.forEach(System.out::println); // will do business process here
}catch (IOException e) {
e.printStackTrace(); // will handle failure case here
}
Run Code Online (Sandbox Code Playgroud)
但是编译器会抛出错误,例如
找不到:值流
◾尝试没有捕获或者最终等同于将其主体放入块中; 没有例外处理.
不确定是什么问题.我是使用Java NIO的新手,所以非常感谢任何帮助.