JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

FileStore in Java 7

January 23, 2014 by Krishna Srinivasan Leave a Comment

FileStore is new class introduced in Java 7.0. It is part of java.nio package. This class helps in geting the type of storage for the files stored whether it’s a device, partition or concreate file system. Also this class defines the list of methods which is useful for getting information about the file storage such as getTotalSpace, getUsableSpace, getUnallocated space. FileStors is an abstract class and can not create instance directly. You can get FileStors instance by calling the FileSystems.getFileStores()  method that will return the FileStore for that particular file. Lets look at the below example:

FileStore Example

[code lang=”java”]
package javabeat.net.core;

import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;

public class FileStoreExample {
static final long KILO_BYTE = 1024;

public static void main(String[] args) throws Exception {
FileSystem fileSystem = FileSystems.getDefault();

for (FileStore fileStore : fileSystem.getFileStores()) {
long totalSpace = fileStore.getTotalSpace() / KILO_BYTE;
long usableSpace = fileStore.getUsableSpace() / KILO_BYTE;
long usedSpace = (fileStore.getTotalSpace() – fileStore
.getUnallocatedSpace()) / KILO_BYTE;
String fsName = fileStore.name();
String fsType = fileStore.type();
boolean readOnly = fileStore.isReadOnly();
System.out.println("File Store Details");
System.out.println("——————");
System.out.println("Total Space : " + totalSpace);
System.out.println("Used Space : " + usedSpace);
System.out.println("Un-Used Space : " + usableSpace);
System.out.println("File Store Name :" + fsName);
System.out.println("File Store Type :" + fsType);
System.out.println("Is this read only : " + readOnly);

}
}
}
[/code]

Output

[code]
File Store Details
——————
Total Space : 104857596
Used Space : 50022036
Un-Used Space : 54835560
File Store Name :OSDisk
File Store Type :NTFS
Is this read only : false
File Store Details
——————
Total Space : 139338748
Used Space : 61134088
Un-Used Space : 78204660
File Store Name :DATA
File Store Type :NTFS
Is this read only : false
[/code]

Filed Under: Java Tagged With: java 7, Java Basics

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved