Igor Chubin
Igor Chubin

Reputation: 64563

How to find file size in scala?

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?

Upvotes: 14

Views: 11776

Answers (3)

dwarfer88
dwarfer88

Reputation: 135

java.nio.file.Files.size

from api:

public static long size(Path path) throws IOException

Returns the size of a file (in bytes)

Upvotes: 3

Ayush Jain
Ayush Jain

Reputation: 19

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

Upvotes: 0

lreeder
lreeder

Reputation: 12206

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

Upvotes: 27

Related Questions