Scala - 如果文件存在,则删除文件,Scala 方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30015165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala - delete file if exist, the Scala way
提问by Johnny
How to nicely delete file in Scala, "the Scala way"?
如何在 Scala 中很好地删除文件,“Scala 方式”?
For example, i can use something like this, very Javastyle:
例如,我可以使用这样的东西,非常Java风格:
private def deleteFile(path: String) = {
val fileTemp = new File(path)
if (fileTemp.exists) {
fileTemp.delete()
}
}
How would it be implemented in Scala, in a more functional syntax?
它如何在 Scala 中以更实用的语法实现?
回答by dk14
You can't get rid of side effects while doing IO-operations, so no good functional ways here. All functional stuff is actually ends when you start to interact with user/devices directly, no monad can help you to do one external side-effect; however, you can describe (wrap) sequential side-effects using IO-like Monads.
做 -IO操作时无法摆脱副作用,所以这里没有好的功能方法。当您开始直接与用户/设备交互时,所有功能性的东西实际上都结束了,没有任何 monad 可以帮助您做一个外部副作用;但是,您可以使用类似IO的 Monad来描述(包装)顺序副作用。
Talking about your example, monad-restyled code may look like:
谈到你的例子,monad-restyled 代码可能如下所示:
implicit class FileMonads(f: File) {
def check = if (f.exists) Some(f) else None //returns "Maybe" monad
def remove = if (f.delete()) Some(f) else None //returns "Maybe" monad
}
for {
foundFile <- new File(path).check
deletedFile <- foundFile.remove
} yield deletedFile
res11: Option[java.io.File] = None
But that's too verbose without any real advantages if you justwant to delete one file. More than that, fileTemp.existscheck has no sense and actually isn't reliable (as @Eduardo pointed out). So, even in Scala the best way I know is FileUtils.deleteQuietly:
但是如果你只想删除一个文件,那太冗长了,没有任何真正的好处。不仅如此,fileTemp.exists检查没有意义,实际上也不可靠(正如@Eduardo 指出的那样)。所以,即使在 Scala 中,我所知道的最好的方法是FileUtils.deleteQuietly:
FileUtils.deleteQuietly(new File(path))
Or even
甚至
new File(path).delete()
It won't throw an exception for non-existent file - just return false.
它不会为不存在的文件抛出异常 - 只需 return false。
If you really want something more Scala-way - look at rapture.iofor example:
如果您真的想要更多 Scala 方式的东西 -例如查看rapture.io:
val file = uri"file:///home/work/garbage"
file.delete()
Or scala-io. More info: How to do File creation and manipulation in functional style?
或Scala-io。更多信息:如何以函数式风格创建和操作文件?
P.S. However IO-monads might be useful (unlike Some/None in my case) when you require asynchronous operations, so naive code (without cats/scalaz) would look like:
PS 但是,当您需要异步操作时,IO-monads 可能很有用(在我的情况下与 Some/None 不同),因此原始代码(没有cats/scalaz)看起来像:
implicit class FileMonads(f: File) {
def check = Future{ f.exists } //returns "Future" monad
def remove = Future{ f.remove } //returns "Future" monad
}
for {
exists <- new File(path).check
_ <- if (exists) foundFile.remove else Future.unit
}
Of course, in the real world, it's best to use some NIO-wrappers like FS2-io: https://lunatech.com/blog/WCl5OikAAIrvQCoc/functional-io-with-fs2-streams
当然,在现实世界中,最好使用一些 NIO-wrappers,如FS2-io:https: //lunatech.com/blog/WCl5OikAAIrvQCoc/functional-io-with-fs2-streams

