Nica Santos
Nica Santos

Reputation: 113

show file information after choosing in file chooser java

I am exploring Jfilechooser.I already get the file path in the Jfilechooser.Now I want to display the information like file name, file size, location, and access rights.Is their anyway to display those information using the file path only.Anyone can help me?I want it to display in a TextArea.

this is the how I pop up a Jfilechooser.

private void browsebuttonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    String filename = f.getAbsolutePath();
    fieldlocation.setText(filename);
}

Upvotes: 1

Views: 383

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61198

Take a look at the JavaDoc for File:

File.getName()

Will return the name of the file

File.length()

Will return the size of the file in bytes

File.getAbsolutePath()

Will return the absolute path of the file

File.canRead()
File.canWrite()
File.canExecute()

Will return your access rights on the file.

One thing I would note about your code is that you don't check the return value from the file chooser. If the user clicks cancel you probably want to abort processing. The way to do this is to check the return value of JFileChoose.showOpenDialog(null); like so:

int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}

Straight from the JavaDoc.

In short I would suggest that you (re)?read the documentation for the APIs you are using. It will save you much time in the long run if you understand your own code.

Upvotes: 2

Related Questions