Sunday, December 8, 2013

File.getPath(), File.getAbsolutePath() and File.getCanonicalPath()

This example show how File.getCanonicalPath() different from getPath() and getAbsolutePath() in Linux to open soft-link file.



It is described in Java doc:
  • getPath() converts this abstract pathname into a pathname string.
    The resulting string uses the default name-separator character to separate the names in the name sequence.
  • getAbsolutePath() Returns the absolute pathname string of this abstract pathname.
    If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.
  • getCanonicalPath()
    Returns the canonical pathname string of this abstract pathname.
    A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).
    Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted.
Enter the code to implement FileChooser of JavaFX to select file, then get its Path, Absolute Path and Canonical Path.
package javafx_filechooser;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX_FileChooser extends Application {

    File file;

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");

        final Label labelFile = new Label();

        Button btn = new Button();
        btn.setText("Open FileChooser'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                FileChooser fileChooser = new FileChooser();

                //Set extension filter
                FileChooser.ExtensionFilter extFilter = new 
                        FileChooser.ExtensionFilter("ALL files (*.*)", "*.*");
                fileChooser.getExtensionFilters().add(extFilter);

                //Show open file dialog
                file = fileChooser.showOpenDialog(null);

                String pathsInfo = "";
                pathsInfo += "getPath(): " + file.getPath() + "\n";
                pathsInfo += "getAbsolutePath(): " + file.getAbsolutePath() + "\n";

                pathsInfo += (new File(file.getPath())).isAbsolute();

                try {
                    pathsInfo += "getCanonicalPath(): " + 
                            file.getCanonicalPath() + "\n";
                } catch (IOException ex) {
                    Logger.getLogger(JavaFX_FileChooser
                            .class.getName()).log(Level.SEVERE, null, ex);
                }

                labelFile.setText(pathsInfo);
            }
        });

        VBox vBox = new VBox();
        vBox.getChildren().addAll(btn, labelFile);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


No comments:

Post a Comment