• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

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

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

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

Category: JavaTag: 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.

Previous Post: « OCPJP 6 Mock Exam – 21
Next Post: OCPJP 8 / OCPJP 7 Exam Details »

Reader Interactions

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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact