How to find file size in scala?

ぐ巨炮叔叔 提交于 2019-12-04 18:42:20

问题


I'm writing a scala script, that need to know a size of a file. How do I do this correctly? In Python I would do

os.stat('somefile.txt').st_size

and in Scala?


回答1:


There is no way to do this using the Scala standard libraries. Without resorting to external libraries, you can use the Java File.length() method do do this. In Scala, this would look like:

import java.io.File
val someFile = new File("somefile.txt")
val fileSize = someFile.length

If you want something Scala-specific, you can use an external framework like scalax.io or rapture.io




回答2:


import java.io.File;
val file:File = new File("your file path");
file.length()



回答3:


java.nio.file.Files.size

from api:

public static long size(Path path) throws IOException

Returns the size of a file (in bytes)



来源:https://stackoverflow.com/questions/20596515/how-to-find-file-size-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!