简体   繁体   中英

Get full file path (without alias) on Lion in Java

I have a file with the absolute path: /Users/ivan/test.txt

String absolutePath = file.getAbsolutePath();

But I need the full path: /Volumes/Macintosh HD/Users/ivan/test.txt ("/" is an alias of "Macintosh HD").

I have already tried using getCanonicalPath and FileSystemView but I always get the same path.

Have a look at the Aliases.h file. Specifically, you want to look at the functions FSIsAliasFile() and FSResolveAlias family of functions, possibly FSResolveAlias() or FSResolveAliasFile() .

I haven't tried this myself, but it looks like something that should give you what you're after.

I think there is no way to get that path with pure Java. Yiou'll have to write a native shared-object.

Maye there are some imporvements with Java-7 and the FileSystem

Finally I did it using Runtime (I only needed the real name folder of the "/" alias in the volume folder:

...
// ls -al command lists the files and the alias
proc = Runtime.getRuntime().exec("ls -al /Volumes");

BufferedReader stdInput = new BufferedReader(new 
InputStreamReader(proc.getInputStream()));

String line;
// match the name between the time and the arrow
Pattern myPatter = Pattern.compile("[0-9]+:[0-9]+ (.*?) -> " + "/");
Matcher matcher;
while ((line = stdInput.readLine()) != null) {
        if (line.indexOf(" -> ") != -1){
            // get the real volume name                    
            matcher = myPatter.matcher(line);
            if (matcher.find())
                return matcher.group(1);                     
        }                    
}
...      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM