Lewis
Lewis

Reputation: 9

Beginner Java: How to write a method to set a variable to 0?

I need to create a Format method (within a class called USBDrive) which resets the capacity of a USBDrive to zero in Java. Any suggestions please?

Upvotes: 0

Views: 389

Answers (2)

TMN
TMN

Reputation: 3070

If you're formatting the drive, won't you want to set the capacity to the size of the drive (not 0)?

Formatting the drive will depend a great deal on what format you want to use. FAT? FAT32? ext[234]? NTFS? Your own custom format? Regardless, you'll need some way to access the USB device directly, using the UMS (USB Mass Storage) protocol.

Good luck!

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298938

I am guessing:

If you just want to erase all files on the disk, use a method like this:

public static void eraseRecursively(final File dir, final boolean eraseDir){
    for(final File item : dir.listFiles()){
        if(item.isDirectory()){
            eraseRecursively(item, true);
        } else{
            item.delete();
        }
    }
    if(eraseDir){
        dir.delete();
    }
}

Call it like this:

eraseRecursively(
    yourUsbRootDir,
    false /* if true, it will try to delete the root dir */
);

If you want to really format the disk, there's no way you can do that from Java, you will have to launch an external process.

Upvotes: 1

Related Questions