Java determine drive pathname


Complete source code below will show you how to determine a pathname is a pathname for drive or not.

*********************************************************************
COMPLETE SOURCE CODE FOR : PathnameIsDrive.java
*********************************************************************


import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;

import java.io.File;

public class PathnameIsDrive
{
public static void main(String[]args)
{
//Create file instance from pathname
//Try change to pathname that match any drive in your computer
//For example C:\\ for C drive.
File myFile=new File("C:\\");

//Check whether the given pathname is drive or not
if(FileSystemView.getFileSystemView().isDrive(myFile))
{
System.out.println("This is a drive");
}
else
{
System.out.println("This is not a drive");
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Java and pathname

Before i go through about file's operation, i want to share with you about "pathname" keyword. In java there has lot of methods play with pathname.

For example :
When we want to create a file instance like below
******************************************
File myFile=new File("c:\\mytext.txt");
******************************************

"c:\\mytext.txt" is a pathname for mytext.txt that locate in c drive.Origin for c:\\mytext.txt is c:\mytext.txt.We use two backslash to prevent syntax error when we compile it.

So, what is pathname ?
  • A pathname is a string that describes what directory the file is in, as well as the name of the file.