I have an application using JFileChooser to select a directory. In this app, I do not want to try to process symbolic links, and I would like to run on multiple platforms. The code therefore attempts to determine whether a chosen file is a symbolic link, and displays an error dialog if it is.
Here is the code that obtains the File from the JFileChooser.
public File getDirectoryChoice(String buttonText, String currentDirectory)
{
File chosenFile = null;
if (fileChooser == null) { fileChooser = new JFileChooser(); }
if (currentDirectory != null)
{ fileChooser.setCurrentDirectory(new File(currentDirectory)); }
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setApproveButtonText(buttonText);
int returnValue = fileChooser.showOpenDialog(mainFrame);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
chosenFile = fileChooser.getSelectedFile();
}
return chosenFile;
}
and here is the code I use to determine whether the chosen file is a symbolic link:
public static boolean isSymbolic(File f)
{
try
{
String absolute = f.getAbsolutePath();
String canonical = f.getCanonicalPath();
return !(absolute.equals(canonical));
}
catch (IOException ioe)
{
return false;
}
}
On Windows 7: If the user chooses a given directory using the mouse, this works fine. If the user types the same directory name into the filename text box, the second code snippet indicates that the absolute and canonical paths are not the same. It does not matter if the user enters the trailing backslash or not.
When I stop this in the debugger on the 'return' statement line and look at the details of the two strings, the hash value of the absolute path string is a big negative number, and the hash value of the canonical string is 0. I don't know why that would be, and in fact wonder if it's a quirk of the (eclipse) debugger.
Can anyone tell me why there would be this difference?