FileStore in Java 7

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

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);

		}
	}
}

Output

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

Comments

comments

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.

Speak Your Mind

*