File size distribution: Difference between revisions
Content added Content deleted
(Update Lang example: Use new set item operator in assignment) |
(New post.) |
||
Line 1,009: | Line 1,009: | ||
1000000 4 |
1000000 4 |
||
10000000 4</syntaxhighlight> |
10000000 4</syntaxhighlight> |
||
=={{header|J}}== |
|||
</syntaxhighlight> |
|||
import java.io.IOException; |
|||
import java.nio.file.Files; |
|||
import java.nio.file.Path; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
public final class FileSizeDistribution { |
|||
public static void main(String[] aArgs) throws IOException { |
|||
List<Path> fileNames = Files.list(Path.of(".")) |
|||
.filter( file -> ! Files.isDirectory(file) ) |
|||
.map(Path::getFileName) |
|||
.toList(); |
|||
Map<Integer, Integer> fileSizes = new HashMap<Integer, Integer>(); |
|||
for ( Path path : fileNames ) { |
|||
fileSizes.merge(String.valueOf(Files.size(path)).length(), 1, Integer::sum); |
|||
} |
|||
final int fileCount = fileSizes.values().stream().mapToInt(Integer::valueOf).sum(); |
|||
System.out.println("File size distribution for directory \".\":" + System.lineSeparator()); |
|||
System.out.println("File size in bytes | Number of files | Percentage"); |
|||
System.out.println("-------------------------------------------------"); |
|||
for ( int key : fileSizes.keySet() ) { |
|||
final int value = fileSizes.get(key); |
|||
System.out.println(String.format("%s%d%s%d%15d%15.1f%%", |
|||
" 10^", ( key - 1 ), " to 10^", key, value, ( 100.0 * value ) / fileCount)); |
|||
} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
File size distribution for directory ".": |
|||
File size in bytes | Number of files | Percentage |
|||
------------------------------------------------- |
|||
10^0 to 10^1 1 0.2% |
|||
10^1 to 10^2 1 0.2% |
|||
10^2 to 10^3 5 1.1% |
|||
10^3 to 10^4 3 0.6% |
|||
10^4 to 10^5 161 34.0% |
|||
10^5 to 10^6 196 41.4% |
|||
10^6 to 10^7 98 20.7% |
|||
10^7 to 10^8 9 1.9% |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |