如何从文件夹中获取上次保存的项目的名称?

时间:2014-02-08 04:56:24

标签: java image swing file

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

1 个答案:

答案 0 :(得分:3)

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

Images Last Modified

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;
    }
}