Java deletes svn files

Source: Internet
Author: User

Many companies now adopt SVN Development, and its version management is indeed good.
However, the source code is quite depressing. there will be many svn files, and many versions will be generated when the Java source code generation or configuration file is changed multiple times. svn may be N times the source code size.
If you want to remove the svn file of a directory, you can write a program to delete it. all files under the svn directory. convenient and use, I use the DirectoryWalker IN THE commons-io.jar here, you can understand the name of "directory walk ".
The Code is as follows:

Package com. ycl. filter. FileCleaner;

Import java. io. File;
Import java. io. IOException;
Import java. util. ArrayList;
Import java. util. Collection;
Import java. util. List;

Import org. apache. commons. io. DirectoryWalker;

Public class FileCleaner extends DirectoryWalker {

Public FileCleaner (){
Super ();
}

Public List <File> clean (File startDirectory) throws IOException {
List <File> results = new ArrayList <File> ();
Walk (startDirectory, results );
Return results;
}

@ Override
Protected void handleStart (File startDirectory, Collection results)
Throws IOException {
System. out. println ("------- start to clear -------");
}

@ Override
Protected void handleEnd (Collection results) throws IOException {
System. out. println ("------- stop clearing -------");
}

@ Override
Protected void handleCancelled (File startDirectory, Collection results,
CancelException cancel) throws IOException {
System. out. println ("------- clear exception -------");
Super. handleCancelled (startDirectory, results, cancel );
}

@ Override
Protected boolean handleIsCancelled (File file, int depth, Collection results)
Throws IOException {
// You can set a breakpoint here. For example, when you find a class, stop traversing and resume by default.
Return false;
}

@ Override
Protected void handleDirectoryStart (File directory, int depth,
Collection results) throws IOException {
// System. out. println ("***** start processing:" + directory. getName () + "deep:" + depth + "results:" + results. toString ());
}

@ Override
Protected void handleDirectoryEnd (File directory, int depth,
Collection results) throws IOException {
// System. out. println ("***** End Processing:" + directory. getName () + "deep:" + depth + "results:" + results. toString ());
}

@ Override
Protected void handleRestricted (File directory, int depth,
Collection results) throws IOException {
System. out. println ("***** restricted directory:" + directory. getName () + "deep:" + depth + "results:" + results. toString ());
}

/**
* Whether to process a directory. false is returned.
*
* @ See Delete. svn directly here.
*/
@ Override
Protected boolean handleDirectory (File directory, int depth,
Collection results ){
// Delete svn directories and then skip
If (". svn". equals (directory. getName ())){
DeleteDirectory (directory, results );
Return false;
} Else {
Results. add (directory); // Delete. svn. Which folders are there?
Return true;
}

}

/**
* Delete an object and add it to the delete list.
*/
@ Override
Protected void handleFile (File file, int depth, Collection results ){
// Delete file and add to list of deleted
// File. delete ();
// Results. add (file );
// What files are there after the. svn file is deleted
}

/**
* Delete folders and files in directories and directories
* @ Param directory
* @ Param results
*/
Private void deleteDirectory (File directory, Collection results ){
If (directory. isDirectory ()){
File [] list = directory. listFiles ();
For (File file: list ){
DeleteDirectory (file, results );
}
}
Log (directory. delete (), directory );
Results. add (directory); // delete a file
}

/**
* Failed file or directory deletion logs
* @ Param flag
*/
Private void Log (boolean flag, File directory ){
If (! Flag ){
System. err. println ("failed to delete file:" + directory. getAbsolutePath ());
} Else {
System. out. println ("delete:" + directory. getAbsolutePath ());
}
}
}


The test code is as follows:

Package com. ycl. filter. FileCleaner;

Import java. io. File;
Import java. io. IOException;
Import java. util. List;

Public class TestFileCleaner {

/**
* @ Param args
* @ Throws IOException
*/
Public static void main (String [] args) throws IOException {
// TODO Auto-generated method stub
FileCleaner cleaner = new FileCleaner ();
File startDirectory = new File ("D: \ workspace \ branches \ pamirsshop_branches_8-30 ");
List <File> list = cleaner. clean (startDirectory );
For (File file: list ){
// System. out. println ("file:" + file. getAbsolutePath ());
}
System. out. println ("total processing" + list. size () + "Files ");

}
}

The test results are as follows:

...
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmain.svn
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnext-baseassembly.xml.svn-base
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnext-base
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnprop-base
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnprops
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnmpext-base
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnmpprop-base
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnmpprops
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnmp
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnall-wcprops
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svnentries
Delete: D: workspaceranchespamirsshop_branches_8-30deploysrcmainassembly.svn
------- End clearing -------
Processing 3331 files in total

I remember when I first encountered this svn problem, I also looked for tools everywhere. Is there any tool I could remove. I think about svn now. I am a programmer. If I encounter any problems, I am not finished developing one myself. It is practical and convenient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.