Thursday, February 7, 2013

AbsolutePath, RealPath and Uri

AbsolutePath, RealPath and Uri
AbsolutePath, RealPath and Uri


package javafx_niofile;

import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
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.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_NIOFile extends Application {

Label label;

@Override
public void start(Stage primaryStage) {
Button btnLoad = new Button();
btnLoad.setText("Load File");
btnLoad.setOnAction(btnLoadEventListener);

label = new Label();

VBox rootBox = new VBox();
rootBox.getChildren().addAll(btnLoad, label);

Scene scene = new Scene(rootBox, 400, 350);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();
}

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

EventHandler<ActionEvent> btnLoadEventListener
= new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent t) {
FileChooser fileChooser = new FileChooser();

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

//Show open file dialog, return a java.io.File object
File file = fileChooser.showOpenDialog(null);

//obtain a java.nio.file.Path object
Path path = file.toPath();

String fileInfo;
fileInfo = "path = " + path + "\n";
try {
fileInfo +=
"AbsolutePath = " + path.toAbsolutePath() + "\n" +
"RealPath = " + path.toRealPath(LinkOption.NOFOLLOW_LINKS) + "\n" +
"Uri = " + path.toUri() + "\n";
} catch (IOException ex) {
Logger.getLogger(JavaFX_NIOFile.class.getName()).log(Level.SEVERE, null, ex);
}

label.setText(fileInfo);
}
};
}


No comments:

Post a Comment