Java Utililty Methods Folder Size Get

List of utility methods to do Folder Size Get

Description

The list of methods to do Folder Size Get are organized into topic(s).

Method

FontemSize(Font font, double em)
em Size
return new Font(font.getName(), font.getStyle(), (int) (font.getSize() * em));
longfolderSize(File directory)
folder Size
long length = 0;
for (File file : directory.listFiles()) {
    if (file.isFile()) {
        length += file.length();
    } else {
        length += folderSize(file);
return length;
longfolderSize(File directory)
Get the size of a directory in bytes
long length = 0;
for (File file : directory.listFiles()) {
    if (file.isFile())
        length += file.length();
    else
        length += folderSize(file);
return length;
...
longfolderSize(File directory)
Recursively traverses a folder and its subfolders to calculate the total size in bytes.
if (directory == null || !directory.exists()) {
    return -1;
if (directory.isDirectory()) {
    long length = 0;
    for (File file : directory.listFiles()) {
        length += folderSize(file);
    return length;
} else if (directory.isFile()) {
    return directory.length();
return 0;
DimensionfontSize(Font f)
{ method
Toolkit g = Toolkit.getDefaultToolkit(); 
if (g == null) {
    return (new Dimension(10, 10));
FontMetrics metrics = g.getFontMetrics(f);
return (new Dimension(metrics.charWidth('W'), metrics.getHeight()));
DimensionfontSize(Font f)
{ method
Toolkit g = Toolkit.getDefaultToolkit(); 
if (g == null) {
    return (new Dimension(10, 10));
FontRenderContext frc = new FontRenderContext(null, false, false);
Rectangle2D bounds = f.getStringBounds("W", frc);
double height = bounds.getHeight();
double width = bounds.getWidth();
...
voidsetFileChooserFont(Component[] comp, int newFontSize)
set File Chooser Font
for (int x = 0; x < comp.length; x++) {
    if (comp[x] instanceof Container)
        setFileChooserFont(((Container) comp[x]).getComponents(), newFontSize);
    try {
        Font original = comp[x].getFont();
        Font newFont = original.deriveFont(Float.valueOf(newFontSize));
        comp[x].setFont(newFont);
    } catch (Exception e) {
...
voidsetFontOfPixelHeight(Graphics2D g, int style, double targetSize)
Sets the font on the given graphics context to have the given style and target size
Font font = new Font("SansSerif", style, 1);
for (int i = 1; i < targetSize; i++) {
    Font bigger = font.deriveFont((float) i);
    g.setFont(bigger);
    if (bigger.getLineMetrics("WBLMNqpyg", g.getFontRenderContext()).getHeight() < targetSize) 
        font = bigger;
    } else {
...
voidsetFontSize(Component c, float size)
Set the font size for a component (leaves other properties of the current font intact)
c.setFont(c.getFont().deriveFont(size));
CsetFontSize(final C component, final int fontSize)
Sets font size of the specified component.
if (component != null && component.getFont() != null) {
    component.setFont(component.getFont().deriveFont((float) fontSize));
return component;