Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2821 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  parcours d'une arraylist

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

parcours d'une arraylist

n°2169231
charlonien
Posté le 27-12-2012 à 17:41:10  profilanswer
 

Bonjour à tous !
J'avais déjà fais appel a vous par le passé et j'en était satisfait .
J'ai donc cette fois-ci un problème d'un autre genre.
J'ai un projet en Java qui consiste a détecter les Pdf a partir d'un chemin donné.
Mon code se compose en plusieurs classe:
 
la première contient le main  c'est elle qui va créer une fenêtre en faisant appel à la classe que j'ai créée
voici donc le main
 

Code :
  1. package pdfFinder;
  2. public class MainPdfFinder {
  3. public static void main(String[] args) {
  4.  new PdfViewer();
  5. }
  6. }

 
 
et maintenant voici ma fenêtre attention c'est gros !  
 

Code :
  1. package pdfFinder;
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.FlowLayout;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTable;
  17. import javax.swing.JTextField;
  18. public class PdfViewer extends JFrame {
  19. private PdfFinder find = new PdfFinder();
  20. private boolean chooseSearch = false;
  21. private boolean chooseViewer = false;
  22. private String searchPath;
  23. private String viewerPath;
  24. private ImageIcon viewerOk = new ImageIcon("ok.png" );
  25. private ImageIcon viewerWrong = new ImageIcon("wrong.png" );
  26. private ImageIcon searchOk = new ImageIcon("ok.png" );
  27. private ImageIcon searchrWrong = new ImageIcon("wrong.png" );
  28. private JPanel north = new JPanel(new GridLayout(3, 1));
  29. private JPanel viewerPan = new JPanel();
  30. private JPanel searchPan = new JPanel();
  31. private JPanel startPan = new JPanel();
  32. private JPanel center = new JPanel();
  33. private JPanel east = new JPanel(new GridLayout(2,1));
  34. private JPanel foundPan = new JPanel(new GridLayout(2, 1));
  35. private JPanel numberPan = new JPanel();
  36. private JPanel numberPdfPan = new JPanel();
  37. private JPanel openPan = new JPanel();
  38. private JPanel south = new JPanel();
  39. private JButton viewerBut = new JButton("Choose" );
  40. private JButton searchBut = new JButton("Choose" );
  41. private JButton startBut = new JButton("Search PDF" );
  42. private JButton openBut = new JButton("Open PDF" );
  43. private JLabel viewerLab = new JLabel("Viewer Path : " );
  44. private JLabel viewerIcon = new JLabel(viewerWrong);
  45. private JLabel searchLab = new JLabel("Search Path : " );
  46. private JLabel searchIcon = new JLabel(searchrWrong);
  47. private JLabel numberLab = new JLabel("Files Found : " );
  48. private JLabel numberPdfLab = new JLabel();
  49. private JLabel southLab = new JLabel(" Create by CHAUMIENNE Charles " );
  50. private JTextField viewerText = new JTextField(20);
  51. private JTextField searchText = new JTextField(20);
  52. private String title[] = {"name", "number of pages", "size", "last modification", "path", "version", "encrypted"};
  53. private JTable tab = new JTable();
  54. public PdfViewer() {
  55.  setTitle("PDF Finder" );
  56.  setSize(570, 585);
  57.  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.  setLocationRelativeTo(null);
  59.  setLayout(new BorderLayout());
  60.  centerPanel();
  61.  southPanel();
  62.  northPanel();
  63.  eastPanel();
  64.  setVisible(true);
  65. }
  66. class viewerListener implements ActionListener{
  67.  public void actionPerformed(ActionEvent a){
  68.   viewerPath = viewerText.getText();
  69.   if(find.verifPath(viewerPath)){
  70.    chooseViewer = true;
  71.    viewerPan.remove(viewerIcon);
  72.    viewerIcon.updateUI();
  73.    viewerIcon = new JLabel(viewerOk);
  74.    viewerPan.add(viewerIcon);
  75.    viewerIcon.updateUI();
  76.    openBut.setEnabled(true);
  77.   }
  78.   else{
  79.    chooseViewer = false;
  80.    viewerPan.remove(viewerIcon);
  81.    viewerIcon.updateUI();
  82.    viewerIcon = new JLabel(viewerWrong);
  83.    viewerPan.add(viewerIcon);
  84.    viewerIcon.updateUI();
  85.    openBut.setEnabled(false);
  86.   }
  87.  }
  88. }
  89. class searchListener implements ActionListener{
  90.  public void actionPerformed(ActionEvent a){
  91.   searchPath = searchText.getText();
  92.   if(find.verifPath(searchPath)){
  93.    chooseSearch = true;
  94.    searchPan.remove(searchIcon);
  95.    searchIcon.updateUI();
  96.    searchIcon = new JLabel(searchOk);
  97.    searchPan.add(searchIcon);
  98.    searchIcon.updateUI();
  99.    startBut.setEnabled(true);
  100.   }
  101.   else{
  102.    chooseSearch = false;
  103.    searchPan.remove(searchIcon);
  104.    searchIcon.updateUI();
  105.    searchIcon = new JLabel(searchrWrong);
  106.    searchPan.add(searchIcon);
  107.    searchIcon.updateUI();
  108.    startBut.setEnabled(false);
  109.   }
  110.  }
  111. }
  112. class StartListener implements ActionListener{
  113.  public void actionPerformed(ActionEvent a){
  114.   try {
  115.    if(chooseSearch == true){
  116.     find.searchAlgo(new File(searchPath));
  117.     numberPdfLab.setText(""+find.getNbPdf());
  118.     find.setNbPdf(0);
  119.     find.afficher();
  120.     center.updateUI();
  121.    }
  122.   } catch (IOException e) {
  123.    e.printStackTrace();
  124.   }
  125.  }
  126. }
  127. class OpenListener implements ActionListener{
  128.  public void actionPerformed(ActionEvent a){
  129.   if( chooseViewer == true){
  130.   }
  131.  }
  132. }
  133. public void northPanel(){
  134.  viewerPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  135.  viewerPan.add(viewerLab);
  136.  viewerPan.add(viewerText);
  137.  viewerBut.addActionListener(new viewerListener());
  138.  viewerPan.add(viewerBut);
  139.  viewerPan.add(viewerIcon);
  140.  north.add(viewerPan);
  141.  searchPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  142.  searchPan.add(searchLab);
  143.  searchPan.add(searchText);
  144.  searchBut.addActionListener(new searchListener());
  145.  startBut.setEnabled(false);
  146.  searchPan.add(searchBut);
  147.  searchPan.add(searchIcon);
  148.  north.add(searchPan);
  149.  startPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  150.  startBut.addActionListener(new StartListener());
  151.  startPan.add(startBut);
  152.  north.add(startPan);
  153.  getContentPane().add(north, BorderLayout.NORTH);
  154. }
  155. public void southPanel(){
  156.  south.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  157.  south.add(southLab);
  158.  getContentPane().add(south, BorderLayout.SOUTH);
  159. }
  160. public void eastPanel(){
  161.  numberPan.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
  162.  numberLab.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
  163.  numberPan.add(numberLab);
  164.  foundPan.add(numberPan);
  165.  numberPdfPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  166.  numberPdfLab.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
  167.  numberPdfPan.add(numberPdfLab);
  168.  foundPan.add(numberPdfPan);
  169.  openPan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  170.  openBut.setEnabled(false);
  171.  openBut.addActionListener(new OpenListener());
  172.  openPan.add(openBut);
  173.  east.add(foundPan);
  174.  east.add(openPan);
  175.  getContentPane().add(east, BorderLayout.EAST);
  176. }
  177. public void centerPanel(){
  178.  center.add(new JScrollPane(tab));
  179.  getContentPane().add(center, BorderLayout.CENTER);
  180. }
  181. }


 
Lorsque le StartListener est appelé il fait appel à une methode SearchAlgo qui est l'algorithme de recherche qui se trouve dans une classe que j'ai nommé PdfFinder et qui contient une aray liste d'une autre classe de ma création qui se nomme Pdf  
 
voici donc PdfFinder
 

Code :
  1. package pdfFinder;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import com.itextpdf.text.pdf.PdfReader;
  7. public class PdfFinder {
  8. private ArrayList<Pdf> lsPdf = new ArrayList<Pdf>();
  9. private String pdfViewerPath;
  10. private int nbPdf =0 ;
  11. public PdfFinder() {
  12. }
  13. public static boolean isPdf(String path) {
  14.  try {
  15.   PdfReader pdfR = new PdfReader(path);
  16.   return true;
  17.  }
  18.  catch (IOException e) {
  19.   return false;
  20.  }
  21. }
  22. public void searchAlgo(File dir) throws IOException {
  23.  File[] file;
  24.  file = dir.listFiles();
  25.  for (int i = 0; i < file.length; i++) {
  26.   if (isPdf(dir.getAbsolutePath())) {
  27.    Pdf pdf = new Pdf(file[i].getAbsolutePath());
  28.    lsPdf.add(pdf);
  29.    afficher(pdf);
  30.    setNbPdf (getNbPdf() + 1 );
  31.   }
  32.   else if (file[i].isDirectory()) {
  33.    searchAlgo(file[i]);
  34.   }
  35.  }
  36. }
  37. public boolean verifPath(String path){
  38.  boolean exist ;
  39.  File file = new File(path);
  40.  exist = file.exists();
  41.  return exist;
  42. }
  43. public Object[][] setData(){
  44.  Object [][] data = new Object[lsPdf.size()][7];
  45.  for(int i=0 ; i < lsPdf.size() ; i++){
  46.   data[i][0] = (lsPdf.get(i).getNameFile());
  47.   data[i][1] = (lsPdf.get(i).getNbPages());
  48.   data[i][2] = (lsPdf.get(i).getSize());
  49.   data[i][3] = (lsPdf.get(i).getLastModif());
  50.   data[i][4] = (lsPdf.get(i).getPath());
  51.   data[i][5] = (lsPdf.get(i).getVersion());
  52.   data[i][6] = (lsPdf.get(i).getIsEncrypted());
  53.  }
  54.  return data;
  55. }
  56. public void afficher(Pdf pdf){
  57.    System.out.println(pdf.toString());
  58. }
  59. public void setNbPdf(int nbPdf){
  60.  this.nbPdf = nbPdf;
  61. }
  62. public int getNbPdf(){
  63.  return nbPdf;
  64. }
  65. }


 
et maintenant la classe Pdf  qui elle contient toutes les caractéristiques d'un Pdf  
 

Code :
  1. package pdfFinder;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.Serializable;
  5. import java.util.Date;
  6. import com.itextpdf.text.pdf.PdfReader;
  7. public class Pdf implements Serializable {
  8. private String nameFile;
  9. private long size;
  10. private String formatedSize;
  11. private int nbPages;
  12. private String path;
  13. private char version;
  14. private boolean isEncrypted;
  15. private Date lastModif;
  16. private PdfReader pdfReader;
  17. private File file;
  18. public Pdf(String path) throws IOException {
  19.  this.path = path;
  20.  file = new File(path);
  21.  nameFile = file.getName();
  22.  pdfReader = new PdfReader(nameFile);
  23.  nbPages = pdfReader.getNumberOfPages();
  24.  size = file.length();
  25.  formatedSize = getFormatedSize(size);
  26.  version = pdfReader.getPdfVersion();
  27.  isEncrypted = pdfReader.isEncrypted();
  28.  lastModif = new Date(file.lastModified());
  29. }
  30. public String getNameFile() {
  31.  return nameFile;
  32. }
  33. public long getSize() {
  34.  return size;
  35. }
  36. public String getFormatedSize (long size){
  37.  int temp;
  38.  size = size/1024;
  39.  if(size > 1000){
  40.   temp = (int)size;
  41.   temp = temp/1000;
  42.   if(size > 1000){
  43.    temp = (int)size;
  44.    temp = temp*1000;
  45.    return temp+" ko" ;
  46.   }
  47.   return temp+"Mo";
  48.  }
  49.  size = size*1000;
  50.  temp = (int)size;
  51.  temp = temp/1000;
  52.  return temp+"Bytes";
  53. }
  54. public int getNbPages() {
  55.  return nbPages;
  56. }
  57. public String getPath() {
  58.  return path;
  59. }
  60. public char getVersion() {
  61.  return version;
  62. }
  63. public boolean getIsEncrypted() {
  64.  return isEncrypted;
  65. }
  66. public Date getLastModif() {
  67.  return lastModif;
  68. }
  69. public PdfReader getPdfReader() {
  70.  return pdfReader;
  71. }
  72. public String toString(){
  73.  return "\n\tname = " + nameFile + "\n\tsize = " + formatedSize
  74.    + "\n\tnumber of pages = " + nbPages + "\n\tpath = " + path
  75.    + "\n\tversion = " + version + "\n\tencrypted = " + isEncrypted
  76.    + "\n\tlast modification = " + lastModif;
  77. }
  78. }

 
 
Le problème est qu'il m'indique  une aray out of bouds exeception a la ligne 66 dans le setData de la classe PdfFinder que je n'ai invoquée nul part et ceci se passe lorsque je clique sur le bouton start de ma fenêtre  
 
Je vous remercie d'avance pour votre aide :)

mood
Publicité
Posté le 27-12-2012 à 17:41:10  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  parcours d'une arraylist

 

Sujets relatifs
Parcours de 2 fichiers[AJAX] Parcours en modalpopupextender avec des users controls
[BASH] parcours récursif et command externe => command not found[RESOLU] Extraire un sous-type d'une arrayList
[C++] Parcours dans un SetArrayList qui se met à jour
Java et arraylist d'objets[C] Structure de données à utiliser pour le parcours de dossier
[BATCH] creation de repertoires par parcours de fichier texteHashmap arraylist et NullpointerExecption
Plus de sujets relatifs à : parcours d'une arraylist


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR