0 Replies Latest reply on Mar 11, 2017 7:34 PM by MML.Pro

    gets files/directories size with MB/GB in JTextField

    MML.Pro

      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 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 
               */
              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                   
      
      
      }
      

       

      8gjy2.png