IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

Recherche de dossiers grâce à une regexp


Sujet :

Entrée/Sortie Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2005
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 14
    Points : 16
    Points
    16
    Par défaut Recherche de dossiers grâce à une regexp
    Bonjour,

    Je recherche comment faire simplement en Java une recherche de dossier à partir d'une regexp.

    Exemple :
    arborescence des dossiers :
    /dossier1/a/sousdossier1/sousdossier2
    /dossier1/b/sousdossier1/sousdossier2
    /dossier1/c/sousdossier1/sousdossier2
    /dossier2/c/sousdossier1/sousdossier2
    /dossier1/c/sousdossier2/sousdossier1

    Regexp à utiliser : /dossier1/*/sousdossier1/sousdossier2

    List des fichiers à retourner :
    /dossier1/a/sousdossier1/sousdossier2
    /dossier1/b/sousdossier1/sousdossier2
    /dossier1/c/sousdossier1/sousdossier2

  2. #2
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2005
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 14
    Points : 16
    Points
    16
    Par défaut
    J'ai trouvé mon bonheur. Avec quelques modifications pour matcher sur le path du fichier et non sur le nom du fichier dans la classe Find.

    Passer par un Observateur ne m'était pas utile, mais pourquoi enlever une fonctionnalité tellement puissante.

    http://forum.java.sun.com/thread.jsp...sageID=9489581

    Code original :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
     
    Find.java
    =======
    /*
     * Created on Jul 8, 2005 by @author Tom Jacobs
     *
     */
    package tjacobs.io;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Observable;
    import java.util.Observer;
    import java.util.Set;
    import java.util.TreeSet;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    //import tjacobs.util.Observable;
     
    /**
     * Find is pretty groovy. The point of it is to find files ala windows search.
     * doesn't have the advanced search options tho.
     * 
     * Extends Observable so all you need to do is add an observer to it.
     */
    public class Find extends Observable implements Runnable {
     
    	private Thread mMyThread;
    	private Pattern mPattern;
    	private String mDirContains;
    	private File mStartDir;
    	private boolean mDescOnly = false;
    	private List<File> mMatches = new ArrayList<File>();
    	private List<File> mDirectoriesToSearch = new ArrayList<File>();
    	private Set<String> mDirectoriesSearched = new TreeSet<String>();
    	private boolean mStop;
    	private boolean mPause;
    	public static final boolean DEBUG = false;
     
    	private Find(Pattern pattern, File dir, boolean desc) {
    		super();
    		mPattern = pattern;
    		mStartDir = dir;
    		mDescOnly = desc;
    	}
     
    	public int size() {
    		return mMatches.size();
    	}
     
    	public File get(int i) {
    		return mMatches.get(i);
    	}
     
    	public Iterator<File> iterator() {
    		return Collections.unmodifiableList(mMatches).iterator();
    	}
     
    	public static Find fsFind(Pattern pattern, File startDir) {
    		return fsFind(pattern, startDir, false);
    	}
     
    	public static Find fsFind(String pattern, File startDir, boolean descendOnly) {
    		return fsFind(Pattern.compile(pattern), startDir, descendOnly);
    	}
     
    	public static Find fsFind(Pattern pattern, File startDir, boolean descendOnly) {
    		Find find = new Find(pattern, startDir, descendOnly);
    		Thread t = new Thread(find, "Find");
    		t.setDaemon(true);
    		t.start();
    		return find;
    	}
     
    	public static Find fsFind(String pattern, File startDir) {
    		return fsFind(Pattern.compile(pattern), startDir); 
    	}
     
    	public static Find fsFind(Pattern pattern, String startDir) {
    		return fsFind(pattern, new File(startDir));
    	}
     
    	public static Find fsFind(String pattern, String startDir) {
    		return fsFind(Pattern.compile(pattern), startDir); 
    	}
     
    	public static Find fsFind(String pattern, String startDir, boolean descendOnly) {
    		return fsFind(Pattern.compile(pattern), startDir, descendOnly); 
    	}
     
    	public static Find fsFind(Pattern pattern, String startDir, boolean descendOnly) {
    		return fsFind(pattern, new File(startDir), descendOnly);
    	}
     
    	public static Find fsFind(String pattern) {
    		return fsFind(Pattern.compile(pattern));
    	}
     
    	public static Find fsFind(Pattern pattern) {
    		return fsFind(pattern, (File)null);
    	}
     
    	public void join() {
    		try {
    			while (mMyThread == null) {
    				Thread.yield();
    			}
    			mMyThread.join();
    		}
    		catch (InterruptedException ex) {
    			//TODO: Log this
    		}
    	}
     
    	public synchronized void addObserver(Observer o) {
    		if (o == null) return;
    		int size = size();
    		for (int i = 0; i < size; i++) {
    			o.update(this, get(i));
    		}
    		super.addObserver(o);
    	}
     
    //		super.addObserver(o);
    //		if (mMatches != null && mMatches.size() > 0) {
    //			for (int i = 0; i < mMatches.size(); i++) {o.update(this, mMatches.)
    //		}
    //	}
     
    	public void run() {
    		mStop = false;
    		mMyThread = Thread.currentThread();
    		if (mStartDir == null) {
    			mStartDir = new File(".");
    		}
    		mStartDir = mStartDir.getAbsoluteFile();
    		if (mStartDir.isDirectory()) {
    			mDirectoriesToSearch.add(mStartDir);
    		}
    		else {
    			mDirectoriesToSearch.add(mStartDir.getParentFile());
    		}
    		while (mDirectoriesToSearch.size() > 0 && !mStop) {
    			if (mPause) {
    				synchronized(this) {
    					try {
    						wait();
    					}
    					catch (Exception ex) {ex.printStackTrace();}
    				}
    			}
    			File toSearch = mDirectoriesToSearch.remove(0);
    			//mDirectoriesSearched.add(toSearch.getAbsolutePath());
    			search(toSearch);
    		}
    	}
     
    	private void addToMatches(File dir) {
    		mMatches.add(dir);
    		notifyObservers(dir);
    	}
     
    	private void search(File dir) {
    		if (DEBUG) System.out.println("dir = " + dir);
    		if (mDirectoriesSearched.contains(dir.getAbsolutePath())) {
    			return;
    		}
    		mDirectoriesSearched.add(dir.getAbsolutePath());
    		if (!mDescOnly) {
    			File par = dir.getParentFile();
    			if (par != null) mDirectoriesToSearch.add(par);
    		}
    		Matcher matcher = mPattern.matcher(dir.getName());
    		if (matcher.matches()) {
    			addToMatches(dir);
    		}
    		boolean shouldMatch = true;
    		if (mDirContains != null) {
    			shouldMatch = dir.getAbsolutePath().toLowerCase().contains(mDirContains);
    		}
    		File files[] = dir.listFiles();
    		if (files == null) {
    			return;
    		}
    		for (int i =0; i < files.length; i++) {
    			if (files[i].isDirectory()) {
    				if (DEBUG) {
    					System.out.println("adding to search: " + files[i]);
    				}
    				mDirectoriesToSearch.add(files[i]);
    			}
    			else if (shouldMatch) {
    				matcher = mPattern.matcher(files[i].getName());
    				if (DEBUG) {
    					System.out.println ("Testing: " + files[i].getName());
    				}
    				//String name = files[i].getName();
    				if (matcher.matches()) {
    					super.setChanged();
    					addToMatches(files[i]);
    				}				
    			}
    		}
    	}
     
    	public void setDirContains(String str) {
    		mDirContains = str.toLowerCase();
    	}
     
    	public String getDirContains() {
    		return mDirContains;
    	}
     
    	public void pause() {
    		mPause = !mPause;
    		if (!mPause) {
    			synchronized(this) {
    				notify();
    			}
    		}
    	}
     
    	public void stop() {
    		mStop = true;
    	}
    }
    Code modifié


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
     
    /*
     * Created on Jul 8, 2005 by @author Tom Jacobs
     *
     */
     
    package tjacobs.io;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Observable;
    import java.util.Observer;
    import java.util.Set;
    import java.util.TreeSet;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    //import tjacobs.util.Observable;
     
     
     
    /**
     * Find is pretty groovy. The point of it is to find files ala windows search.
     * doesn't have the advanced search options tho.
     * 
     * Extends Observable so all you need to do is add an observer to it.
     */
    public class Find extends Observable implements Runnable {
     
    	private Thread mMyThread;
    	private Pattern mPattern;
    	private String mDirContains;
    	private File mStartDir;
    	private boolean mDescOnly = false;
    	private List mMatches = new ArrayList();
    	private List mDirectoriesToSearch = new ArrayList();
    	private Set mDirectoriesSearched = new TreeSet();
    	private boolean mStop;
    	private boolean mPause;
    	public static final boolean DEBUG = false;
     
    	Find(Pattern pattern, File dir, boolean desc) {
    		super();
    		mPattern = pattern;
    		mStartDir = dir;
    		mDescOnly = desc;
    	}
     
    	public int size() {
    		return mMatches.size();
    	}
     
    	public File get(int i) {
    		return (File)mMatches.get(i);
    	}
     
    	public Iterator iterator() {
    		return Collections.unmodifiableList(mMatches).iterator();
    	}
     
    	public static Find fsFind(Pattern pattern, File startDir) {
    		return fsFind(pattern, startDir, false);
    	}
     
    	public static Find fsFind(String pattern, File startDir, boolean descendOnly) {
    		return fsFind(Pattern.compile(pattern), startDir, descendOnly);
    	}
     
    	public static Find fsFind(Pattern pattern, File startDir, boolean descendOnly) {
    		Find find = new Find(pattern, startDir, descendOnly);
    		Thread t = new Thread(find, "Find");
    		t.setDaemon(true);
    		t.start();
    		return find;
    	}
     
    	public static Find fsFind(String pattern, File startDir) {
    		return fsFind(Pattern.compile(pattern), startDir); 
    	}
     
    	public static Find fsFind(Pattern pattern, String startDir) {
    		return fsFind(pattern, new File(startDir));
    	}
     
    	public static Find fsFind(String pattern, String startDir) {
    		return fsFind(Pattern.compile(pattern), startDir); 
    	}
     
    	public static Find fsFind(String pattern, String startDir, boolean descendOnly) {
    		return fsFind(Pattern.compile(pattern), startDir, descendOnly); 
    	}
     
    	public static Find fsFind(Pattern pattern, String startDir, boolean descendOnly) {
    		return fsFind(pattern, new File(startDir), descendOnly);
    	}
     
    	public static Find fsFind(String pattern) {
    		return fsFind(Pattern.compile(pattern));
    	}
     
    	public static Find fsFind(Pattern pattern) {
    		return fsFind(pattern, (File)null);
    	}
     
    	public void join() {
    		try {
    			while (mMyThread == null) {
    				Thread.yield();
    			}
    			mMyThread.join();
    		}
    		catch (InterruptedException ex) {
    			//TODO: Log this
    		}
    	}
     
    	public synchronized void addObserver(Observer o) {
    		if (o == null) return;
    		int size = size();
    		for (int i = 0; i < size; i++) {
    			o.update(this, get(i));
    		}
    		super.addObserver(o);
    	}
     
    //		super.addObserver(o);
    //		if (mMatches != null && mMatches.size() > 0) {
    //			for (int i = 0; i < mMatches.size(); i++) {o.update(this, mMatches.)
    //		}
    //	}
     
    	public void run() {
    		mStop = false;
    		mMyThread = Thread.currentThread();
    		if (mStartDir == null) {
    			mStartDir = new File(".");
    		}
    		mStartDir = mStartDir.getAbsoluteFile();
    		if (mStartDir.isDirectory()) {
    			mDirectoriesToSearch.add(mStartDir);
    		}
    		else {
    			mDirectoriesToSearch.add(mStartDir.getParentFile());
    		}
    		while (mDirectoriesToSearch.size() > 0 && !mStop) {
    			if (mPause) {
    				synchronized(this) {
    					try {
    						wait();
    					}
    					catch (Exception ex) {ex.printStackTrace();}
    				}
    			}
    			File toSearch = (File) mDirectoriesToSearch.remove(0);
    			//mDirectoriesSearched.add(toSearch.getAbsolutePath());
    			search(toSearch);
    		}
    	}
     
    	private void addToMatches(File dir) {
    		mMatches.add(dir);
    		notifyObservers(dir);
    	}
     
    	private void search(File dir) {
    		if (DEBUG) System.out.println("dir = " + dir);
    		if (mDirectoriesSearched.contains(dir.getAbsolutePath())) {
    			return;
    		}
    		mDirectoriesSearched.add(dir.getAbsolutePath());
    		if (!mDescOnly) {
    			File par = dir.getParentFile();
    			if (par != null) mDirectoriesToSearch.add(par);
    		}
    		Matcher matcher = mPattern.matcher(dir.getAbsolutePath());
    		if (matcher.matches()) {
    			addToMatches(dir);
    		}
    		boolean shouldMatch = true;
    		if (mDirContains != null) {
    			shouldMatch = contains(dir.getAbsolutePath().toLowerCase(), mDirContains);
    		}
    		File files[] = dir.listFiles();
    		if (files == null) {
    			return;
    		}
    		for (int i =0; i < files.length; i++) {
    			if (files[i].isDirectory()) {
    				if (DEBUG) {
    					System.out.println("adding to search: " + files[i]);
    				}
    				mDirectoriesToSearch.add(files[i]);
    			}
    			if (shouldMatch) {
    				matcher = mPattern.matcher(files[i].getAbsolutePath());
    				if (DEBUG) {
    					System.out.println ("Testing: " + files[i].getAbsolutePath());
    				}
    				//String name = files[i].getName();
    				if (matcher.matches()) {
    					super.setChanged();
    					addToMatches(files[i]);
    				}				
    			}
    		}
    	}
     
    	public void setDirContains(String str) {
    		mDirContains = str.toLowerCase();
    	}
     
    	public String getDirContains() {
    		return mDirContains;
    	}
     
    	public void pause() {
    		mPause = !mPause;
    		if (!mPause) {
    			synchronized(this) {
    				notify();
    			}
    		}
    	}
     
    	public void stop() {
    		mStop = true;
    	}
     
    	/**
         * Returns true if and only if this string contains the specified
         * sequence of char values.
         *
         * @param s the sequence to search for
         * @return true if this string contains <code>s</code>, false otherwise
         * @throws NullPointerException if <code>s</code> is <code>null</code>
         * @since 1.5
         */
        public boolean contains(String st, CharSequence s) {
            return st.indexOf(s.toString()) > -1;
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    package tjacobs.io;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Observable;
    import java.util.Observer;
     
    public class FindObserver implements Observer {
     
    	private List lDossier = new ArrayList();
     
    	public void update(Observable o, Object arg) {
    		lDossier.add(arg);
    	}
     
    	public List getLDossier() {
    		return lDossier;
    	}
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    Pattern p = Pattern.compile(".*\\\\sousdossier1\\\\sousdossier2");
     
    Find f = new Find( p , new File( "\\dossier1\\" ), true);
    FindObserver fo = new FindObserver() ;
    f.addObserver( fo );
    f.run();
     
    List lDossier = fo.getLDossier();

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Recherche d'une période grâce à une date
    Par freygeo dans le forum SAS Base
    Réponses: 5
    Dernier message: 03/07/2012, 13h40
  2. Réponses: 6
    Dernier message: 05/05/2012, 01h56
  3. [RegEx] ordre de la recherche d'une regexp
    Par bruno.rotrou dans le forum Langage
    Réponses: 2
    Dernier message: 05/04/2012, 14h26
  4. Réponses: 21
    Dernier message: 07/05/2006, 17h27
  5. [C#][xml][regexp] Recherche d'une regexp dans un fichier xml
    Par LeJocker dans le forum Windows Forms
    Réponses: 4
    Dernier message: 18/10/2005, 12h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo