893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A *
893N/A * Redistribution and use in source and binary forms, with or without
893N/A * modification, are permitted provided that the following conditions
893N/A * are met:
893N/A *
893N/A * - Redistributions of source code must retain the above copyright
893N/A * notice, this list of conditions and the following disclaimer.
893N/A *
893N/A * - Redistributions in binary form must reproduce the above copyright
893N/A * notice, this list of conditions and the following disclaimer in the
893N/A * documentation and/or other materials provided with the distribution.
893N/A *
2362N/A * - Neither the name of Oracle nor the names of its
893N/A * contributors may be used to endorse or promote products derived
893N/A * from this software without specific prior written permission.
893N/A *
893N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
893N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
893N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
893N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
893N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
893N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
893N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
893N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
893N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
893N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
893N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
893N/A */
893N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * Example utility that works like the df(1M) program to print out disk space
893N/A * information
893N/A */
893N/A
893N/Apublic class DiskUsage {
893N/A
893N/A static final long K = 1024;
893N/A
893N/A static void printFileStore(FileStore store) throws IOException {
3471N/A long total = store.getTotalSpace() / K;
3471N/A long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / K;
3471N/A long avail = store.getUsableSpace() / K;
893N/A
893N/A String s = store.toString();
893N/A if (s.length() > 20) {
893N/A System.out.println(s);
893N/A s = "";
893N/A }
893N/A System.out.format("%-20s %12d %12d %12d\n", s, total, used, avail);
893N/A }
893N/A
893N/A public static void main(String[] args) throws IOException {
893N/A System.out.format("%-20s %12s %12s %12s\n", "Filesystem", "kbytes", "used", "avail");
893N/A if (args.length == 0) {
893N/A FileSystem fs = FileSystems.getDefault();
893N/A for (FileStore store: fs.getFileStores()) {
893N/A printFileStore(store);
893N/A }
893N/A } else {
893N/A for (String file: args) {
3471N/A FileStore store = Files.getFileStore(Paths.get(file));
893N/A printFileStore(store);
893N/A }
893N/A }
893N/A }
893N/A}