1

I was trying to find the size of the file in kotlin, using path.file size . It is crashing with file not found exception.

file.getname is working find and is giving me the file name.

any leads would be appreciated.

Tariq Hussain
  • 409
  • 1
  • 5
  • 19

2 Answers2

1

Us these set of extension functions to get the size.

val File.size get() = if (!exists()) 0.0 else length().toDouble()
val File.sizeInKb get() = size / 1024
val File.sizeInMb get() = sizeInKb / 1024
val File.sizeInGb get() = sizeInMb / 1024
val File.sizeInTb get() = sizeInGb / 1024

Ref: SO Question

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
1

There are different ways to get the file size you can implement your own extension method and other way is

 Formatter.formatShortFileSize(context, File(absolutePath).length())
Tariq Hussain
  • 409
  • 1
  • 5
  • 19