Listing Last 10 Modified Files In Directory
in
Programming Questions
•
1 month ago
Hi,
and here is the adapted code;
Im using the following code to get the last image in a directory, but i wish to get the last 9 images.
I have adapted this code for 9 images
this is just the part of the code i am having issues with currently.
- import java.io.*;
- FilenameFilter jpgFilter = new FilenameFilter() {
- boolean accept(File dir, String name) {
- return name.toLowerCase().endsWith(".jpg");
- }
- };
- PImage img = null;
- void setup() {
- size(1200, 900);
- }
- void draw() {
- if (img != null) {
- image(img, 0, 0);
- }
- }
- void folderSelected(File folder) {
- if (folder == null) {
- println("Window was closed or the user hit cancel.");
- }
- else {
- File[] files = folder.listFiles(jpgFilter);
- long timeTest = 0;
- File lastImage = null;
- for (int i = 0; i < files.length; i++) {
- if (files[i].lastModified() > timeTest) {
- timeTest = files[i].lastModified();
- lastImage = files[i];
- }
- println(files[i] + " : " + files[i].lastModified());
- }
- println();
- println(lastImage.getPath());
- img = loadImage(lastImage.getPath());
- }
- }
- void keyPressed() {
- if (key == ' ') {
- selectFolder("Select a folder to process:", "folderSelected");
- }
- }
and here is the adapted code;
- import java.io.*;
- FilenameFilter jpgFilter = new FilenameFilter() {
- boolean accept(File dir, String name) {
- return name.toLowerCase().endsWith(".jpg");
- }
- };
- PImage img1 = null;
- PImage img2 = null;
- PImage img3 = null;
- PImage img4 = null;
- PImage img5 = null;
- PImage img6 = null;
- PImage img7 = null;
- PImage img8 = null;
- PImage img9 = null;
- void setup() {
- size(1200, 900);
- }
- void draw() {
- if (img1 != null) {
- image(img1, 0, 0);
- }
- if (img2 != null) {
- image(img2, 0, 0);
- }
- if (img3 != null) {
- image(img3, 0, 0);
- }
- if (img4 != null) {
- image(img4, 0, 0);
- }
- if (img5 != null) {
- image(img5, 0, 0);
- }
- if (img6 != null) {
- image(img6, 0, 0);
- }
- if (img7 != null) {
- image(img7, 0, 0);
- }
- if (img8 != null) {
- image(img8, 0, 0);
- }
- if (img9 != null) {
- image(img9, 0, 0);
- }
- }
- void folderSelected(File folder) {
- if (folder == null) {
- println("Window was closed or the user hit cancel.");
- }
- else {
- File[] files = folder.listFiles(jpgFilter);
- long timeTest = 0;
- File lastImage1 = null;
- File lastImage2 = null;
- File lastImage3 = null;
- File lastImage4 = null;
- File lastImage5 = null;
- File lastImage6 = null;
- File lastImage7 = null;
- File lastImage8 = null;
- File lastImage9 = null;
- for (int i = 0; i < files.length; i++) {
- if (files[i].lastModified() > timeTest) {
- timeTest = files[i].lastModified();
- lastImage1 = files[i];
- lastImage2 = files[i-1];
- lastImage3 = files[i-2];
- lastImage4 = files[i-3];
- lastImage5 = files[i-4];
- lastImage6 = files[i-5];
- lastImage7 = files[i-6];
- lastImage8 = files[i-7];
- lastImage9 = files[i-8];
- }
- println(files[i] + " : " + files[i].lastModified());
- }
- println();
- println(lastImage1.getPath());
- img1 = loadImage(lastImage1.getPath());
- img2 = loadImage(lastImage2.getPath());
- img3 = loadImage(lastImage3.getPath());
- img4 = loadImage(lastImage4.getPath());
- img5 = loadImage(lastImage5.getPath());
- img6 = loadImage(lastImage6.getPath());
- img7 = loadImage(lastImage7.getPath());
- img8 = loadImage(lastImage8.getPath());
- img9 = loadImage(lastImage9.getPath());
- }
- }
- void keyPressed() {
- if (key == ' ') {
- selectFolder("Select a folder to process:", "folderSelected");
- }
- }
and the errors...
now i think the issue is the for loop starting at 0, and trying to -1 from it, but i don't know how to get around it.java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at processing.core.PApplet.selectCallback(PApplet.java:6697)at processing.core.PApplet.access$000(PApplet.java:158)at processing.core.PApplet$6.run(PApplet.java:6684)at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)at java.awt.EventQueue.access$400(EventQueue.java:82)at java.awt.EventQueue$2.run(EventQueue.java:676)at java.awt.EventQueue$2.run(EventQueue.java:674)at java.security.AccessController.doPrivileged(Native Method)at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)Caused by: java.lang.ArrayIndexOutOfBoundsException: -1at sketch_130820d.folderSelected(sketch_130820d.java:98)... 21 more
or if there is a plainer way?
thanks!
1