Thursday, January 24, 2013

Get file info using java.io.File

Get file info
Get file info


package javafx_fileops;

import java.io.File;
import java.io.IOException;
import java.util.Date;
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_FileOps 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
            File file = fileChooser.showOpenDialog(null);

            String fileInfo = 
                    "AbsoluteFile: " + file.getAbsoluteFile() + " of " + file.getAbsoluteFile().getClass() + "\n"
                    + "ParentFile: " + file.getParentFile()  + " of " + file.getParentFile().getClass() + "\n"
                    + "AbsolutePath: " + file.getAbsolutePath() + "\n"
                    + "Path: " + file.getPath() + "\n"
                    + "Name: " + file.getName() + "\n"
                    + "Parent: " + file.getParent() + "\n"
                    + "canExecute- " + file.canExecute() + "\n"
                    + "canRead- " + file.canRead() + "\n"
                    + "canWrite- " + file.canWrite() + "\n"
                    + "isAbsolute- " + file.isAbsolute() + "\n"
                    + "isDirectory- " + file.isDirectory() + "\n"
                    + "isFile- " + file.isFile() + "\n"
                    + "isHidden- " + file.isHidden() + "\n"
                    + "lastModified: " + (new Date(file.lastModified())).toString() + "\n"
                    + "length: " + file.length() + "\n"
                    + "URI: " + file.toURI()  + " of " + file.toURI().getClass() + "\n";
            try {
                fileInfo += "\n";
                fileInfo += "CanonicalFile: " + file.getCanonicalFile() + " of " + file.getCanonicalFile().getClass() + "\n";
                fileInfo += "CanonicalPath: " + file.getCanonicalPath() + "\n";
            } catch (IOException ex) {
                Logger.getLogger(JavaFX_FileOps.class.getName()).log(Level.SEVERE, null, ex);
            }

            label.setText(fileInfo);
        }
    };
}


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

4 comments:

  1. Good example, but you can use PrintWriter and println()-methode (line 61). Then the code would be a little bit clearer...

    ReplyDelete
    Replies
    1. hello,

      Thx for your advice.

      But I want to display in the application window, for easy capture and show in the picture.

      Delete
  2. This is not a good example. You should use NIO.2.

    ReplyDelete