Hi All,
I created example of gui it's get information about the file and/or directories that chosen by the user via JFilechooser, in JTextField named (File_Size) I used this method in this link that gives the size for files and/or directories with Megabytes but I replaced double with float , When I run I see it's working fine with files, but When I select large directory (about Gigabytes) almost returns 0.0 or number this number 0.00390625, I also tried this methods fileName.getTotalSpace() / 1024 and this method also the same result, see the pic to understand the program
package files_dir.information;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author Muhammed_Pro
*/
public class Files_Dir_Information extends javax.swing.JFrame {
/\*\*
\* Creates new form Files\_information
\*/
public Files\_Dir\_Information() {
initComponents();
this.setVisible(true);
this.setResizable(false);
}
/\*\*
\* This method is called from within the constructor to initialize the form.
\* WARNING: Do NOT modify this code. The content of this method is always
\* regenerated by the Form Editor.
\*/
@SuppressWarnings("unchecked")
// \<editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
getInformation = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
File\_path = new javax.swing.JTextField();
Can\_write = new javax.swing.JTextField();
Can\_read = new javax.swing.JTextField();
Last\_modify = new javax.swing.JTextField();
Parent\_dir = new javax.swing.JTextField();
File\_Size = new javax.swing.JTextField();
Is\_file = new javax.swing.JTextField();
Is\_directory = new javax.swing.JTextField();
BrowseFiles = new javax.swing.JButton();
Clear = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT\_ON\_CLOSE);
setTitle("Files / Directories Info");
jPanel1.setPreferredSize(new java.awt.Dimension(700, 530));
getInformation.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
getInformation.setText("get information");
getInformation.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
getInformationActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel2.setText("File path");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel3.setText("Can write");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel4.setText("Can read");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel5.setText("Last modify");
jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel6.setText("Parent directory");
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel7.setText("File size");
jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel8.setText("Is file");
jLabel9.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel9.setText("Is directory");
File\_path.setEditable(false);
File\_path.setBackground(new java.awt.Color(0, 0, 0));
File\_path.setForeground(new java.awt.Color(0, 255, 51));
Can\_write.setEditable(false);
Can\_write.setBackground(new java.awt.Color(0, 0, 0));
Can\_write.setForeground(new java.awt.Color(0, 255, 51));
Can\_read.setEditable(false);
Can\_read.setBackground(new java.awt.Color(0, 0, 0));
Can\_read.setForeground(new java.awt.Color(0, 255, 51));
Last\_modify.setEditable(false);
Last\_modify.setBackground(new java.awt.Color(0, 0, 0));
Last\_modify.setForeground(new java.awt.Color(0, 255, 51));
Parent\_dir.setEditable(false);
Parent\_dir.setBackground(new java.awt.Color(0, 0, 0));
Parent\_dir.setForeground(new java.awt.Color(0, 255, 51));
File\_Size.setEditable(false);
File\_Size.setBackground(new java.awt.Color(0, 0, 0));
File\_Size.setForeground(new java.awt.Color(0, 255, 51));
Is\_file.setEditable(false);
Is\_file.setBackground(new java.awt.Color(0, 0, 0));
Is\_file.setForeground(new java.awt.Color(0, 255, 51));
Is\_directory.setEditable(false);
Is\_directory.setBackground(new java.awt.Color(0, 0, 0));
Is\_directory.setForeground(new java.awt.Color(0, 255, 51));
BrowseFiles.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
BrowseFiles.setText("Browse Files/Dir");
BrowseFiles.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BrowseFilesActionPerformed(evt);
}
});
Clear.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Clear.setText("Clear");
Clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Parent\_dir, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Can\_write, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(File\_path, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Clear, javax.swing.GroupLayout.PREFERRED\_SIZE, 166, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(BrowseFiles, javax.swing.GroupLayout.PREFERRED\_SIZE, 459, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(getInformation, javax.swing.GroupLayout.PREFERRED\_SIZE, 166, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel7)
.addComponent(jLabel8)
.addComponent(jLabel9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Last\_modify, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addComponent(File\_Size, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addComponent(Is\_file, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addComponent(Is\_directory, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Can\_read, javax.swing.GroupLayout.PREFERRED\_SIZE, 337, javax.swing.GroupLayout.PREFERRED\_SIZE)))
.addContainerGap(19, Short.MAX\_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component\[\] {jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7, jLabel8, jLabel9});
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component\[\] {Can\_read, Can\_write, File\_Size, File\_path, Is\_directory, Is\_file, Last\_modify, Parent\_dir});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BrowseFiles)
.addComponent(getInformation))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(File\_path, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addComponent(Clear))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(Can\_write, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Can\_read, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addComponent(jLabel4))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(Last\_modify, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(Parent\_dir, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(File\_Size, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(Is\_file, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(Is\_directory, javax.swing.GroupLayout.PREFERRED\_SIZE, 26, javax.swing.GroupLayout.PREFERRED\_SIZE))
.addContainerGap(37, Short.MAX\_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component\[\] {getInformation, jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7, jLabel8, jLabel9});
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component\[\] {Can\_read, Can\_write, File\_Size, File\_path, Is\_directory, Is\_file, Last\_modify, Parent\_dir});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT\_SIZE, javax.swing.GroupLayout.DEFAULT\_SIZE, Short.MAX\_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED\_SIZE, javax.swing.GroupLayout.DEFAULT\_SIZE, javax.swing.GroupLayout.PREFERRED\_SIZE)
.addGap(0, 0, Short.MAX\_VALUE))
);
pack();
setLocationRelativeTo(null);
}// \</editor-fold>
private void getInformationActionPerformed(java.awt.event.ActionEvent evt) {
if (File\_path.getText().isEmpty()) {
JOptionPane.showMessageDialog(Parent\_dir, "Please Select a Valid File Path", "Error", JOptionPane.ERROR\_MESSAGE);
} else {
try {
File fileName = new File(File\_path.getText());
File\_path.setText(fileName.getAbsolutePath());
Can\_write.setText(fileName.canWrite() ? "Yes" : "No");
Can\_read.setText(fileName.canRead() ? "Yes" : "No");
Last\_modify.setText("" + new Date(fileName.lastModified()));
Parent\_dir.setText(fileName.getParent());
if (fileName.exists()) {
float bytes = fileName.length();
float kilobytes = (bytes / 1024);
float megabytes = (kilobytes / 1024);
File\_Size.setText("MegaBytes : " + megabytes);
} else if (fileName.isDirectory()) {
float bytes = fileName.length();
float kilobytes = (bytes / 1024f);
float megabytes = (kilobytes / 1024f);
File\_Size.setText("MegaBytes : " + megabytes);
} else {
File\_Size.setText("File dose not exists");
}
Is\_file.setText(fileName.isFile() ? "Yes" : "No");
Is\_directory.setText(fileName.isDirectory() ? "Yes" : "No");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
private void BrowseFilesActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES\_AND\_DIRECTORIES);
int returnFile = fc.showOpenDialog(null);
if (returnFile == JFileChooser.APPROVE\_OPTION) {
File file = fc.getSelectedFile();
File\_path.setText(file.getAbsolutePath());
}
}
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {
File\_path.setText(null);
Can\_write.setText(null);
Can\_read.setText(null);
Last\_modify.setText(null);
Parent\_dir.setText(null);
File\_Size.setText(null);
Is\_file.setText(null);
Is\_directory.setText(null);
}
/\*\*
\* [@param](https://forums.oracle.com/ords/apexds/user/param) args the command line arguments
\*/
public static void main(String args\[\]) {
/\* Set the Nimbus look and feel \*/
//\<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/\* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
\* For details see [http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html](http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html)
\*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Files\_Dir\_Information.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Files\_Dir\_Information.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Files\_Dir\_Information.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Files\_Dir\_Information.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//\</editor-fold>
//\</editor-fold>
/\* Create and display the form \*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Files\_Dir\_Information().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton BrowseFiles;
private javax.swing.JTextField Can\_read;
private javax.swing.JTextField Can\_write;
private javax.swing.JButton Clear;
private javax.swing.JTextField File\_Size;
private javax.swing.JTextField File\_path;
private javax.swing.JTextField Is\_directory;
private javax.swing.JTextField Is\_file;
private javax.swing.JTextField Last\_modify;
private javax.swing.JTextField Parent\_dir;
private javax.swing.JButton getInformation;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
