public FatFileSystem format ( ) throws IOException { final int sectorSize = device . getSectorSize ( ) ; final int totalSectors = ( int ) ( device . getSize ( ) / sectorSize ) ; final FsInfoSector fsi ; final BootSector bs ; if ( sectorsPerCluster == 0 ) throw new AssertionError ( ) ; if ( fatType == FatType . FAT32 ) { bs = new Fat32BootSector ( device ) ; initBootSector ( bs ) ; final Fat32BootSector f32bs = ( Fat32BootSector ) bs ; f32bs . setFsInfoSectorNr ( 1 ) ; f32bs . setSectorsPerFat ( sectorsPerFat ( 0 , totalSectors ) ) ; final Random rnd = new Random ( System . currentTimeMillis ( ) ) ; f32bs . setFileSystemId ( rnd . nextInt ( ) ) ; f32bs . setVolumeLabel ( label ) ; /* create FS info sector */ fsi = FsInfoSector . create ( f32bs ) ; } else { bs = new Fat16BootSector ( device ) ; initBootSector ( bs ) ; final Fat16BootSector f16bs = ( Fat16BootSector ) bs ; final int rootDirEntries = rootDirectorySize ( device . getSectorSize ( ) , totalSectors ) ; f16bs . setRootDirEntryCount ( rootDirEntries ) ; f16bs . setSectorsPerFat ( sectorsPerFat ( rootDirEntries , totalSectors ) ) ; if ( label != null ) f16bs . setVolumeLabel ( label ) ; fsi = null ; } final Fat fat = Fat . create ( bs , 0 ) ; final AbstractDirectory rootDirStore ; if ( fatType == FatType . FAT32 ) { rootDirStore = ClusterChainDirectory . createRoot ( fat ) ; fsi . setFreeClusterCount ( fat . getFreeClusterCount ( ) ) ; fsi . setLastAllocatedCluster ( fat . getLastAllocatedCluster ( ) ) ; fsi . write ( ) ; } else { rootDirStore = Fat16RootDirectory . create ( ( Fat16BootSector ) bs ) ; } final FatLfnDirectory rootDir = new FatLfnDirectory ( rootDirStore , fat , false ) ; rootDir . flush ( ) ; for ( int i = 0 ; i < bs . getNrFats ( ) ; i ++ ) { fat . writeCopy ( bs . getFatOffset ( i ) ) ; } bs . write ( ) ; /* possibly write boot sector copy */ if ( fatType == FatType . FAT32 ) { Fat32BootSector f32bs = ( Fat32BootSector ) bs ; f32bs . writeCopy ( device ) ; } FatFileSystem fs = FatFileSystem . read ( device , false ) ; if ( label != null ) { fs . setVolumeLabel ( label ) ; } fs . flush ( ) ; return fs ; }
Initializes the boot sector and file system for the device . The file system created by this method will always be in read - write mode .