3

Is there a way to get disk partition and volume information using Java libraries only? I also need deleted file information. Disk is formatted as a FAT-16 and has standard MBR.

I need the following information:

a) Partition information - Display the number of partitions on the disk and for each partition display the start sector, size of partition and file system type.

b) Volume information – For the first partition only, display the number of sectors per cluster, the size of the FAT area, the size of the Root Directory, and the sector address of Cluster #2.

c) Deleted file information - For the first deleted file on the volume’s root directory, display the name and size of that file, and the number of the first cluster. Display the first 16 characters of the content of that file (assume it is a simple text file).

Scadge
  • 9,380
  • 3
  • 30
  • 39
  • Clearly they expect you to perform a raw read of partition table, the BIOS parameter block, and the root directory. – Ben Voigt Nov 07 '13 at 00:39

2 Answers2

4

If you need to Getting file system details in Java try This:

import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.io.File;
import javax.swing.filechooser.FileSystemView;

public class Main {

    public static void main(String[] args) {

        System.out.println("File system roots returned byFileSystemView.getFileSystemView():");
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File[] roots = fsv.getRoots();
        for (int i = 0; i < roots.length; i++) {
            System.out.println("Root: " + roots[i]);
        }

        System.out.println("Home directory: " + fsv.getHomeDirectory());

        System.out.println("File system roots returned by File.listRoots():");
        File[] f = File.listRoots();
        for (int i = 0; i < f.length; i++) {
            System.out.println("Drive: " + f[i]);
            System.out.println("Display name: " + fsv.getSystemDisplayName(f[i]));
            System.out.println("Is drive: " + fsv.isDrive(f[i]));
            System.out.println("Is floppy: " + fsv.isFloppyDrive(f[i]));
            System.out.println("Readable: " + f[i].canRead());
            System.out.println("Writable: " + f[i].canWrite());
            System.out.println("Total space: " + f[i].getTotalSpace());
            System.out.println("Usable space: " + f[i].getUsableSpace());
        }
    }
}

Quoted from this answer:

Using JNA, you can call Win32 Kernel32's GetVolumeInformation() to retrieve lpFileSystemNameBuffer parameter which receives the name of the file system, for example, the FAT file system or the NTFS file system

Community
  • 1
  • 1
Alya'a Gamal
  • 5,624
  • 19
  • 34
  • Thank you, but this is not exactly what I need. I edited my post at the top so you can see now what details I want to get. – Scadge Mar 27 '13 at 10:37
  • 2
    Your answer will only work in systems that use separate root for every partition, so it will not work on any Unix-like system (including Linux and MacOSX: the only root is `/`) or on newer Windows (while most partitions have their own roots, some of them can be mounted as a directory). – Karol S Aug 14 '14 at 11:00
-1

you can get partition size in GB or MB

long gb=1024 * 1024 * 1024;

divide by actual size of the partition System.out.println("Total space: " + f[i].getTotalSpace()/gb+"GB"); System.out.println("Usable space: " + f[i].getUsableSpace()/gb+"GB");

  • what does `long gb=102410241024` mean? Is the 102410241024 a random number? Also, in this ` f[i].getTotalSpace()` what is `f`, where do you get it from? – AlexT Feb 05 '22 at 03:30
  • i define * in after the 1024 example 1024 * 1024 * 1024 and f is our list of all Disk partitions divided. f[i].getTotalSapace() is getting total size of disk partition. – vijay kumar Feb 05 '22 at 06:17
  • and where do you get the 1024 from, how is that number related to the question of how to get partition and volume info on a 8 year old question? And for `f` being the list of disk, how did you create that list? – AlexT Feb 05 '22 at 14:47
  • 1024 is the unit that converts Byte to GB, it's easy to calculate the byte to GB so easy to understand the size.it's optional. f is the array of root list(means our system disk partitions ) java have by default static function in File class listRoots() return the array of partitions – vijay kumar Feb 05 '22 at 17:56
  • 1
    ok, you don't seem to get what I mean here. Your code does not show in any way how to get the size of the disk, nor does it show how to get the volumes. Which is what the question is about. Your answer is not an answer, nobody asked how to convert from MB to GB or etc. Have a look at the already existing answer. – AlexT Feb 05 '22 at 18:35