-2

I'm trying to make a boolean that is true if it's on one particular USB drive, and false if it is on any other drive, removable or otherwise.

public class USBChecker {

  private final boolean isMyUSBDrive = somecondition;

  public static void check(String[] args) {
    if (isMyUSBDrive) {
      System.out.println("Yay! I like you.");
    } else {
      System.err.println("Return me to my owner, or I'll self destruct!");
    }
  }

}

Thanks in advance.

Anonymous
  • 491
  • 2
  • 12
  • Welcome to Stack Overflow. Please have a read of [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and update your question accordingly. In particular, please clarify what part of the problem you are having difficulty with and what research and attempts you have made to solve the problem yourself. – Adrian Sanguineti Nov 16 '16 at 03:11
  • 1
    @RobotKarel314, I don't disagree with you and can see where you think my statement was generic. But I also don't think OP is after listing files and directories. I think they are after how to find all drives connected to the computer and what type they are, which is what I would like OP to clarify. – Adrian Sanguineti Nov 16 '16 at 03:44

1 Answers1

0

In the case of OS X, USBs are on /Volumes/USB_NAME, so what I'd do is I'd start with this code

package PACKAGE_NAME;
private boolean myUSBDrive;
public static void main(String[] args) {
    ClassLoader loader = CLASS_NAME.class.getClassLoader()
    if (loader.getResource("PACKAGE_NAME/CLASS_NAME").substring(0,9).equals("/Volumes/")) {
    isMyUSBDrive = true;
    }
    else {
        isMyUSBDrive = false;
    }

Edit: Adrian makes a good point in that, CD's and other USB drives are in Volumes, your post wants true if it's on one particular USB Drive, so change the substring length to however long you need and the string to "/Volumes/USB_NAME/".

MacStation
  • 411
  • 4
  • 20
  • I don't think this would necessarily work because please correct me if I'm wrong, but aren't also CDs and mounted drive images found under volumes? I don't see anything in this code which filters for those. – Adrian Sanguineti Nov 16 '16 at 03:47
  • 1
    You could change the substring length to be longer and be `"/Volumes/USB_NAME" since he wanted it on one specific USB drive and false anywhere else. – MacStation Nov 16 '16 at 17:37
  • Thanks for the help. This will certainly suffice. – Anonymous Nov 17 '16 at 15:43