Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8288882

JFileChooser - empty (0 bytes) file is displayed as 1 KB

XMLWordPrintable

    • b20
    • x86_64
    • linux

      ADDITIONAL SYSTEM INFORMATION :
      "Previously worked in the Release": Never worked?

      * Linux: bug
      * Windows: OK?
      * Probably any Java version

      A DESCRIPTION OF THE PROBLEM :
      An empty (0 bytes) file is displayed as 1 KB in the "Details" view.

      File choosers in other UI toolkits (Qt, GTK, native win32) display the size
      correctly as "0 B" or "0 bytes".

      There is a suspicious line in src/java.desktop/share/classes/sun/swing/FilePane.java:

      MessageFormat.format(kiloByteString, (len == 0L) ? 1L : len);

      REGRESSION : Last worked in version 8

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      0. Screenshot: https://i.imgur.com/1LguaBq.png
      1. Run the attached test on *Linux*
      2. Click the "Details" button in right-top corner
      3. Size of the "TEST-EMPTY-FILE.txt" is displayed as "1 KB" instead of "0 KB"
         (other sizes are displayed correctly - probably)

      Note that Metal Look And Feel on *MS Windows* displays "0 bytes",
      because it uses ShellFolder to set column values.


      ---------- BEGIN SOURCE ----------
      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import javax.swing.JFileChooser;
      import javax.swing.SwingUtilities;

      public class Test {

      public static void main(String[] args) throws IOException {

      Path currentDir = Path.of(System.getProperty("user.dir"));

      // create empty file

      Path emptyFile = currentDir.resolve("TEST-EMPTY-FILE.txt");

      if (!Files.exists(emptyFile)) {
      System.out.println("Creating " + emptyFile);
      Files.createFile(emptyFile);
      }

      // show file chooser

      SwingUtilities.invokeLater(() -> {
      JFileChooser fc = new JFileChooser();
      fc.setCurrentDirectory(currentDir.toFile());
      fc.showOpenDialog(null);
      });
      }

      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. From_submitter.PNG
          85 kB
          Praveen Narayanaswamy
        2. Test.java
          0.7 kB
          Praveen Narayanaswamy
        3. Ubuntu20.04.PNG
          43 kB
          Praveen Narayanaswamy
        4. Windows10.PNG
          58 kB
          Praveen Narayanaswamy

            abhiscxk Abhishek Kumar (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: