Java FileStore diskFree()

Here you can find the source of diskFree()

Description

disk Free

License

Open Source License

Declaration

public static String diskFree() 

Method Source Code

//package com.java2s;
/**/*  www .  j a v  a2  s.  c  om*/
 * @author David Garratt
 * 
 * Project Name : Commander4j
 * 
 * Filename     : JUtility.java
 * 
 * Package Name : com.commander4j.util
 * 
 * License      : GNU General Public License
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * http://www.commander4j.com/website/license.html.
 * 
 */

import java.io.File;

import java.net.URI;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static String diskFree() {
        String result = "";
        long free = 0;
        File f = new File(System.getProperty("user.dir"));
        String root = "";

        while (f.getParent() != null) {
            root = f.getParent();
            f = new File(root);
        }

        try {
            URI rootURI = new URI("file:///");
            Path rootPath = Paths.get(rootURI);
            Path dirPath = rootPath.resolve(root);
            FileStore dirFileStore = Files.getFileStore(dirPath);

            free = dirFileStore.getUsableSpace() / 1048576;
            result = String.valueOf(free) + " mb on " + root;

        } catch (Exception e) {
            result = "Error trying to determine free disk space " + e.getLocalizedMessage();
        }

        return result;
    }
}

Related

  1. canExecuteExecutable(File file)
  2. createTempFileWithAttributes(Path tempFilePath, File fileName)
  3. ensureExists(Path folder)
  4. findTopResourceInVolume(FileStore fstore, Path path)
  5. getAclAttributes(Path file)
  6. getFileStore(Path path)