How to find file size in scala?

2019-03-25 11:23发布

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?

2条回答
闹够了就滚
2楼-- · 2019-03-25 12:09

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

查看更多
爷的心禁止访问
3楼-- · 2019-03-25 12:13

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

查看更多
登录 后发表回答