Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 716
Igor Chubin Avatar asked Dec 15 '13 16:12

Igor Chubin


People also ask

How do you find the file size?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

How do I check a bytes file?

Using the ls Command–l – displays a list of files and directories in long format and shows the sizes in bytes.

How to display file size in java?

In Java, we can use Files. size(path) to get the size of a file in bytes.


2 Answers

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

like image 105
lreeder Avatar answered Oct 20 '22 08:10

lreeder


java.nio.file.Files.size

from api:

public static long size(Path path) throws IOException

Returns the size of a file (in bytes)

like image 30
dwarfer88 Avatar answered Oct 20 '22 07:10

dwarfer88