【问题标题】:How to get the name of the last saved item from a folder?如何从文件夹中获取最后保存的项目的名称?
【发布时间】:2014-02-08 04:56:24
【问题描述】:

我的项目包括一个名为 Image 的文件夹,其中包含图像。我想从中获取图像的总数以及最后保存的图像的名称。有人帮忙吗?

【问题讨论】:

    标签: java image swing file


    【解决方案1】:

    获取文件列表,然后按上次修改日期对它们进行排序。诚然,“最后修改”与“创建日期”不同,但对于大多数情况来说应该足够了。有关列出文件和获取日期的详细信息,请参阅File API 的方法。

    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    class ImagesLastModified {
    
        public static void main(String[] args) {
            File f = new File(System.getProperty("user.home"));
    
            final File[] files = f.listFiles(new ImageFileNameFilter());
            Arrays.sort(files, new FileDateComparator());
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    JPanel gui = new JPanel(new BorderLayout());
    
                    JLabel l = new JLabel("Images: " + files.length);
                    gui.add(l, BorderLayout.PAGE_START);
    
                    JList<File> list = new JList<File>(files);
                    list.setCellRenderer(new FileListCellRenderer());
                    gui.add(new JScrollPane(list));
    
                    JOptionPane.showMessageDialog(null, gui);
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
            SwingUtilities.invokeLater(r);
        }
    }
    
    
    class FileListCellRenderer extends DefaultListCellRenderer {
    
        @Override
        public Component getListCellRendererComponent(
                JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {
            Component c = super.getListCellRendererComponent(
                    list, value, index, isSelected, cellHasFocus);
    
            if (c instanceof JLabel && value instanceof File) {
                JLabel l = (JLabel)c;
                File f = (File)value;
                l.setText(new Date(f.lastModified()) + " " + f.getName());
            }
    
            return c;
        }
    }
    
    class ImageFileNameFilter implements FilenameFilter {
        String[] suffixes = ImageIO.getReaderFileSuffixes();
    
        @Override
        public boolean accept(File dir, String name) {
            String s = name.toLowerCase();
            for (String suffix : suffixes) {
                if (s.endsWith(suffix.toLowerCase())) return true;
            }
            return false;
        }
    }
    
    
    class FileDateComparator  implements Comparator {
    
        @Override
        public int compare(Object o1, Object o2) {
            if (!(o1 instanceof File) || !(o2 instanceof File)) {
                throw new IllegalArgumentException("Both objects must be files!");
            }
            File f1 = (File)o1;
            File f2 = (File)o2;
            int i = (f1.lastModified()>f2.lastModified() ? -1 : 1);
            return i;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 2022-06-15
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多