Saturday, January 26, 2013

Get file info using java.nio.file.Path and java.nio.file.Files

The previous post describe how to "Get file info using java.io.File", this one have similar function using java.nio.file.Path and java.nio.file.Files.

Please note that JavaFX FileChooser return a java.io.File object. To obtain coresponding java.nio.file.Path object, call its toPath() method.

package javafx_niofile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
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 = "Path = " + path.toString() + "\n\n";
            
            fileInfo += "FileSystem: " + path.getFileSystem() + "\n\n";
            
            fileInfo += "FileName = " + path.getFileName() + "\n"
                    + "Parent = " + path.getParent() + "\n\n"
                    + "isExecutable(): " + Files.isExecutable(path) + "\n"
                    + "isReadable(): " + Files.isReadable(path) + "\n"
                    + "isWritable(): " + Files.isWritable(path) + "\n";
            try {
                fileInfo += "getLastModifiedTime(): " + Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS) + "\n"
                        + "size(): " + Files.size(path) + "\n";
            } catch (IOException ex) {
                Logger.getLogger(JavaFX_NIOFile.class.getName()).log(Level.SEVERE, null, ex);
            }

            label.setText(fileInfo);

        }
    };
    
}

Get file info using java.nio.file.Path and java.nio.file.Files
Get file info using java.nio.file.Path and java.nio.file.Files


Related:
- Access subpath of java.nio.file.Path

No comments:

Post a Comment