Mes TD de programmation orientée objet (INSA Toulouse)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Dossier.java 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import java.util.* ;
  2. public class Dossier extends Node {
  3. // Liste des éléments contenus
  4. ArrayList<Node> elements = new ArrayList<Node>();
  5. public Dossier (String path, int depth) {
  6. super(path, 0, depth);
  7. this.depth = depth;
  8. Iterator<String> son;
  9. String p;
  10. long s = 0;
  11. long sum = 0;
  12. try {
  13. son = FileInfo.getElements(path);
  14. }
  15. catch (java.io.IOException e) {
  16. son = null;
  17. }
  18. while (son.hasNext()) {
  19. p = son.next();
  20. try {
  21. s = FileInfo.size(p);
  22. sum += s;
  23. }
  24. catch (java.io.IOException e) {}
  25. if (this.depth + 1 <= maxDepth && nbNode <= maxNode){
  26. if (FileInfo.isFile(p)) {
  27. elements.add(new Fichier(p, s, this.depth + 1));
  28. }
  29. else
  30. elements.add(new Dossier(p, this.depth + 1));
  31. }
  32. }
  33. Collections.sort(this.elements, comparator);
  34. this.size = sum;
  35. }
  36. Comparator<Node> comparator = new Comparator<Node>() {
  37. @Override public int compare(Node node1, Node node2) {
  38. return node1.name.compareTo(node2.name);
  39. }
  40. };
  41. @Override public void afficher() {
  42. System.out.println("+ (DIR) " + this.name + "[total = " + size + " octets]");
  43. for (Node i : elements) {
  44. if (i.depth <= maxDepth){
  45. for (int j = 0; j < depth; ++j){
  46. System.out.print(" ");
  47. }
  48. i.afficher();
  49. }
  50. }
  51. }
  52. }