Intégration d'une classe qui permet le tri des noms de dossier par date de création
Bonjour,
Dans mon code principal, utilisé pour un appareillage, je liste le contenu d'un répertoire. Je souhaiterais adapter le code ci-dessous à mon script plus bas.
Je n'arrive pas à mes fins tout seul.
Le code est sous Java ETK1.4.5 (il ne m'est pas possible de changer cette version)
Voici le code qui me permettrais de lister par date de création:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| File directory = new File("c:\\books\\");
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator() {
public int compare(Object o1, Object o2) {
File a1=(File)o1;
File a2=(File)o2;
if (a1.lastModified() < a2.lastModified())
return -1;
else if (a1.lastModified() > a2.lastModified())
return 1;
else
return 0;
}
}); |
Voici mon code actuel fonctionnel qui me permet de lister le contenu d'un repertoire
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import com.ewon.ewonitf.EvtWebFormListener;
import java.io.PrintStream;
import java.util.Date;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* https://stackoverflow.com/questions/72185256/java-1-4-sort-files-recent-latest
* @author eWonSupport_Adm
*/
public class MyWebFormListener extends EvtWebFormListener
{
public void callForm(String FormName)
{
PrintStream ps = new PrintStream(this);
setWebHeader("text/plain");
// List File
if ("list".equals(FormName))
{
try
{
String FileConnStr = "file:///" + getWebVar("directory","/");
String directoryStr=getWebVar("directory","");
ps.println("directoryStr: "+directoryStr);
FileConnection fc = (FileConnection) Connector.open(FileConnStr);
if (fc.exists() && fc.isDirectory())
{
Enumeration x = fc.list();
FileConnection fc1 = null;
String sz_list = "";
String orderStr = getWebVar("order","");
String typeStr = getWebVar("type","");
String lineBreakStr = getWebVar("lineBreak","");
while(x.hasMoreElements())
{
String item = x.nextElement().toString();
fc1 = (FileConnection) Connector.open(FileConnStr+ "/" + item);
// http://192.168.0.10/rcgi.bin/jvmForm?formName=list&type=file&lineBreak=yes&order=ascending&directory=/usr
// list file
if ("file".equals(typeStr))
{
if (!fc1.isDirectory())
{
if ("yes".equals(lineBreakStr))
{
sz_list += "/" + item+ "\n";
}
else
sz_list += "/" + item + ";";
}
}
// http://192.168.0.10/rcgi.bin/jvmForm?formName=list&type=dir&lineBreak=yes&order=ascending&directory=/usr
// list dir
if ("dir".equals(typeStr))
{
if (fc1.isDirectory())
{
if ("yes".equals(lineBreakStr))
{
sz_list += "/" + item+ "\n";
}
else
sz_list += "/" + item + ";";
}
}
ps.print((sz_list));
}
else
ps.print("DIRECTORY DOES NOT EXIST");
}
catch(Exception ex)
{
System.out.println("size Command Error: " + ex.getMessage());
ps.print("list Command Error");
}
}
}
} |