/**
* Copyright (C) 2001 JRA This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option) any later version. This
* program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass
* Ave, Cambridge, MA 02139, USA.
*/
package
com.ledruide.utils.file;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
/**
* Titre : Directory Toolkit Description : DirectoryToolkit permet de travailler rapidement sur les
* repertoires.<br>
* Deplacement, copie, supression.<br>
* La meme chose est possible sur les fichiers.
*
* @author JRA
* @version 1.0
*/
public
class
DirectoryToolkit {
/**
* Constructeur, ne doit jamais servir donc private !
*/
private
DirectoryToolkit() {}
/**
* Permet de creer un repertoire, si ce repertoire existe deja il ne fait rien.<br>
* Par contre cette methode cree automatiquement toute l'arborescence du repertoire a creer si
* celle-ci n'existe pas.
*
* @param aCheminComplet du (ou des) repertoire(s) a creer
* @throws Exception si une erreur survient
*/
public
static
void
createDirectory(
final
String aCheminComplet )
throws
Exception {
final
File lDirectory =
new
File( aCheminComplet );
if
( !lDirectory.canRead() ) {
lDirectory.mkdirs();
}
}
/**
* Effacer un repertoire et son contenu. Exemple : <BR>
* DirectoryToolkit.deleteDirectory("c:/dev/myProject");<BR>
*
* @param aCheminComplet du repertoire a effacer
* @throws Exception If an error occurs
*/
public
static
void
deleteDirectory(
final
String aCheminComplet )
throws
Exception {
deleteDirectory(
new
File( aCheminComplet ) );
}
/**
* Effacer un repertoire et son contenu. Exemple : <BR>
* DirectoryToolkit.deleteDirectory(new File("c:/dev/myProject"));<BR>
* Cette methode s'apelle recursivement pour suprimer son contenu
*
* @param aDirectory L'objet File representant un repertoire
* @throws Exception If an error occurs
*/
public
static
void
deleteDirectory(
final
File aDirectory )
throws
Exception {
final
File[] lFiles = aDirectory.listFiles();
if
( lFiles !=
null
) {
for
(
int
i =
0
; i < lFiles.length ; ++i ) {
if
( lFiles&#
91
;i&#
93
;.isDirectory() ) {
deleteDirectory( lFiles&#
91
;i&#
93
; );
}
else
{
lFiles&#
91
;i&#
93
;.delete();
}
}
}
aDirectory.delete();
}
/**
* Effacer seulement le contenu d'un répertoire et optionellement de ses sous-répertoires.<br>
* Exemple (dans ce cas, tous les fichiers sont supprimés: <BR>
* <code>DirectoryToolkit.deleteDirectoryContent(new File("c:/dev/myProject"), null, true);</code>
* <BR>
* <br>
* Avec un filtre sur "csv" (seul les fichier possédant "csv" dans leur nom seront supprimés)
* :<BR>
* <code>DirectoryToolkit.deleteDirectoryContent(new File("c:/dev/myProject"), "csv", true);</code>
*
* @param aDirectory L'objet File représentant un répertoire
* @param aFiltre Le filtre pour la suppression des fichiers (null pour tout effacer).
* @param aSousRepertoires true efface tout les sous repertoires.
* @throws Exception If an error occurs
*/
public
static
void
deleteDirectoryContent(
final
File aDirectory ,
final
String aFiltre ,
final
boolean
aSousRepertoires )
throws
Exception {
final
File[] lFiles = aDirectory.listFiles();
if
( lFiles !=
null
) {
for
(
int
i =
0
; i < lFiles.length ; ++i ) {
if
( aSousRepertoires && lFiles&#
91
;i&#
93
;.isDirectory() ) {
deleteDirectoryContent( lFiles&#
91
;i&#
93
; , aFiltre , aSousRepertoires );
}
else
{
if
( aFiltre ==
null
) {
lFiles&#
91
;i&#
93
;.delete();
}
else
{
if
( lFiles&#
91
;i&#
93
;.getName().indexOf( aFiltre ) != -
1
) {
lFiles&#
91
;i&#
93
;.delete();
}
}
}
}
}
}
/**
* @see DirectoryToolkit#deleteDirectoryContent(File aDirectory, String aFiltre, boolean
* aSousRepertoires)
* @param aCheminComplet du (ou des) répertoire(s) é effacer
* @param aFiltre Le filtre pour la suppression des fichiers (null pour tout éffacer).
* @param aSousRepertoires true efface tout les sous repertoires.
* @throws Exception si une erreur survient
*/
public
static
void
deleteDirectoryContent(
final
String aCheminComplet ,
final
String aFiltre ,
final
boolean
aSousRepertoires )
throws
Exception {
deleteDirectoryContent(
new
File( aCheminComplet ) , aFiltre , aSousRepertoires );
}
/**
* Déplacer un répertoire entier et ses sous-répertoire, exemple :<BR>
* DirectoryToolkit.moveDirectory("c:/dev/", "c:/LOCAL/amoi/", "testsJava");<BR>
*
* @param aCheminActuel
* @param aCheminDestination
* @param aRepertoire
* @throws Exception If an error occurs
*/
public
static
void
moveDirectory(
final
String aCheminActuel ,
final
String aCheminDestination ,
final
String aRepertoire )
throws
Exception {
copyDirectory( aCheminActuel , aCheminDestination , aRepertoire );
deleteDirectory( aCheminActuel + aRepertoire );
}
/**
* Copier un répertoire entier sans effacer le contenu de l'original, exemple :<BR>
* DirectoryToolkit.copyDirectory("c:/dev/", "c:/LOCAL/amoi/", "testsJava");<BR>
*
* @param aCheminActuel
* @param aCheminDestination
* @param aRepertoire
* @throws Exception If an error occurs
*/
public
static
void
copyDirectory(
final
String aCheminActuel ,
final
String aCheminDestination ,
final
String aRepertoire )
throws
Exception {
copyDirectory(
new
File( aCheminActuel + aRepertoire ) ,
new
File( aCheminDestination + aRepertoire ) );
}
/**
* Copier un répertoire entier sans effacer le contenu de l'original, exemple :<BR>
* DirectoryToolkit.copyDirectory("c:/dev/", "c:/LOCAL/amoi/", "testsJava");<BR>
*
* @param aRepertoireActuel
* @param aCheminDestination
* @throws Exception Si une erreur survient
*/
public
static
void
copyDirectory(
final
File aRepertoireActuel ,
final
File aCheminDestination )
throws
Exception {
final
File[] lFiles = aRepertoireActuel.listFiles();
File lFile;
File lTemp;
final
String lSEPARATOR = System.getProperty(
"lFile.separator"
);
if
( lFiles !=
null
) {
for
(
int
i =
0
; i < lFiles.length ; ++i ) {
lFile = lFiles&#
91
;i&#
93
;;
System.out.println(
new
StringBuffer().append(
"lFile.getName = "
).append( lFile.getName() )
.toString() );
System.out.println(
new
StringBuffer().append(
"lFile.getParent = "
).append( lFile.getParent() )
.toString() );
if
( lFile.isDirectory() ) {
lTemp =
new
File(
new
StringBuffer().append( aCheminDestination ).append( lSEPARATOR ).append(
lFile.getName() ).toString() );
lTemp.mkdirs();
copyDirectory( lFile , lTemp );
}
else
{
if
( !aCheminDestination.exists() ) {
createDirectory( aCheminDestination.getAbsolutePath() );
}
lTemp =
new
File(
new
StringBuffer().append( aCheminDestination.getAbsolutePath() ).append(
lSEPARATOR ).append( lFile.getName() ).toString() );
copyFile( lFile.getAbsolutePath() , lTemp.getAbsolutePath() );
}
}
}
}
private
static
void
copyFile(
final
String aSourceFile ,
final
String aTargetFile )
throws
Exception {
final
File lInputFile =
new
File( aSourceFile );
final
File lOutputFile =
new
File( aTargetFile );
final
FileInputStream lIn =
new
FileInputStream( lInputFile );
final
FileOutputStream lOut =
new
FileOutputStream( lOutputFile );
int
c;
while
( ( c = lIn.read() ) != -
1
)
lOut.write( c );
lIn.close();
lOut.close();
}
/**
* Renomer un répertoire. Exemple :<BR>
* DirectoryToolkit.renameDirectory("c:/dev/", "projectToto", "TotoProject");<BR>
*
* @param aChemin
* @param aNomActuel
* @param aNouveauNom
* @throws Exception If an error occurs
*/
public
static
void
renameDirectory(
final
String aChemin ,
final
String aNomActuel ,
final
String aNouveauNom )
throws
Exception {
final
File lActual =
new
File( aChemin + aNomActuel );
lActual.renameTo(
new
java.io.File( aChemin + aNouveauNom ) );
}
}