1

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);
}
Nica Santos
  • 113
  • 1
  • 2
  • 9

1 Answers1

2

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.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • how about getting the file type like Text document, Microsoft Word,Excel, PNG etc.I need the name of the file type ,not the file extension (ppt,txt,png).is their a way?i read the JavaDoc ,i didn't find it – Nica Santos Oct 15 '13 at 07:08
  • A file doesn't know it's `type`. On windows, as you probably know, program association is done solely by extension - if you change the extension of a `.html` to `.xlsx` then excel will try to open it. On mac/unix things are little more complicated but there is still no magic bullet. In Java 7 there is [`Files.probeContentType(path)`](http://stackoverflow.com/a/8973468/2071828) which tries to get the mime type - you may have some success with that. – Boris the Spider Oct 15 '13 at 11:50