BAEL-7092: How to get the size of a file in MB, KB & GB in java (#15117)
* BAEL-7092: How to get the size of a file in MB, KB & GB in java * Update JavaFileSizeUnitTest.java * BAEL-7092: How to get the size of a file in MB, KB & GB in java --------- Co-authored-by: Grzegorz Piwowarek <gpiwowarek@gmail.com>
This commit is contained in:
parent
1a555cd9a7
commit
0838279c6a
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.size;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FileSizeUtils {
|
||||
public static long getFileSizeInBytes(File file) {
|
||||
if (file.exists()) {
|
||||
return file.length();
|
||||
} else {
|
||||
throw new IllegalArgumentException("File not found.");
|
||||
}
|
||||
}
|
||||
|
||||
public static double getFileSizeInKilobytes(File file) {
|
||||
long bytes = getFileSizeInBytes(file);
|
||||
return (double) bytes / 1024;
|
||||
}
|
||||
|
||||
public static double getFileSizeInMegabytes(File file) {
|
||||
double kilobytes = getFileSizeInKilobytes(file);
|
||||
return kilobytes / 1024;
|
||||
}
|
||||
|
||||
public static double getFileSizeInGigabytes(File file) {
|
||||
double megabytes = getFileSizeInMegabytes(file);
|
||||
return megabytes / 1024;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package com.baeldung.size;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
@ -10,9 +12,9 @@ import java.net.URL;
|
|||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class JavaFileSizeUnitTest {
|
||||
private static final long EXPECTED_FILE_SIZE_IN_BYTES = 11;
|
||||
|
@ -85,4 +87,23 @@ public class JavaFileSizeUnitTest {
|
|||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, stream.available());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSizeInDifferentUnits_thenCorrect(){
|
||||
filePath = String.join(File.separator, new String[] { "src", "test", "resources", "size", "sample_file_1.in" });
|
||||
File file = new File(filePath);
|
||||
if (file.exists()) {
|
||||
long expectedBytes = file.length();
|
||||
double expectedKilobytes = (double) expectedBytes / 1024;
|
||||
double expectedMegabytes = expectedKilobytes / 1024;
|
||||
double expectedGigabytes = expectedMegabytes / 1024;
|
||||
|
||||
assertEquals(expectedBytes, FileSizeUtils.getFileSizeInBytes(file));
|
||||
assertEquals(expectedKilobytes, FileSizeUtils.getFileSizeInKilobytes(file), 0.01);
|
||||
assertEquals(expectedMegabytes, FileSizeUtils.getFileSizeInMegabytes(file), 0.01);
|
||||
assertEquals(expectedGigabytes, FileSizeUtils.getFileSizeInGigabytes(file), 0.01);
|
||||
} else {
|
||||
fail("File not found.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue