Source Code Cross Referenced for FileDeleteStrategy.java in  » Library » apache-common-IO » org » apache » commons » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common IO » org.apache.commons.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.io;
018:
019:        import java.io.File;
020:        import java.io.IOException;
021:
022:        /**
023:         * Strategy for deleting files.
024:         * <p>
025:         * There is more than one way to delete a file.
026:         * You may want to limit access to certain directories, to only delete
027:         * directories if they are empty, or maybe to force deletion.
028:         * <p>
029:         * This class captures the strategy to use and is designed for user subclassing.
030:         *
031:         * @author Stephen Colebourne
032:         * @version $Id: FileDeleteStrategy.java 453903 2006-10-07 13:47:06Z scolebourne $
033:         * @since Commons IO 1.3
034:         */
035:        public class FileDeleteStrategy {
036:
037:            /**
038:             * The singleton instance for normal file deletion, which does not permit
039:             * the deletion of directories that are not empty.
040:             */
041:            public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy(
042:                    "Normal");
043:            /**
044:             * The singleton instance for forced file deletion, which always deletes,
045:             * even if the file represents a non-empty directory.
046:             */
047:            public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy();
048:
049:            /** The name of the strategy. */
050:            private final String name;
051:
052:            //-----------------------------------------------------------------------
053:            /**
054:             * Restricted constructor.
055:             *
056:             * @param name  the name by which the strategy is known
057:             */
058:            protected FileDeleteStrategy(String name) {
059:                this .name = name;
060:            }
061:
062:            //-----------------------------------------------------------------------
063:            /**
064:             * Deletes the file object, which may be a file or a directory.
065:             * All <code>IOException</code>s are caught and false returned instead.
066:             * If the file does not exist or is null, true is returned.
067:             * <p>
068:             * Subclass writers should override {@link #doDelete(File)}, not this method.
069:             *
070:             * @param fileToDelete  the file to delete, null returns true
071:             * @return true if the file was deleted, or there was no such file
072:             */
073:            public boolean deleteQuietly(File fileToDelete) {
074:                if (fileToDelete == null || fileToDelete.exists() == false) {
075:                    return true;
076:                }
077:                try {
078:                    return doDelete(fileToDelete);
079:                } catch (IOException ex) {
080:                    return false;
081:                }
082:            }
083:
084:            /**
085:             * Deletes the file object, which may be a file or a directory.
086:             * If the file does not exist, the method just returns.
087:             * <p>
088:             * Subclass writers should override {@link #doDelete(File)}, not this method.
089:             *
090:             * @param fileToDelete  the file to delete, not null
091:             * @throws NullPointerException if the file is null
092:             * @throws IOException if an error occurs during file deletion
093:             */
094:            public void delete(File fileToDelete) throws IOException {
095:                if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
096:                    throw new IOException("Deletion failed: " + fileToDelete);
097:                }
098:            }
099:
100:            /**
101:             * Actually deletes the file object, which may be a file or a directory.
102:             * <p>
103:             * This method is designed for subclasses to override.
104:             * The implementation may return either false or an <code>IOException</code>
105:             * when deletion fails. The {@link #delete(File)} and {@link #deleteQuietly(File)}
106:             * methods will handle either response appropriately.
107:             * A check has been made to ensure that the file will exist.
108:             * <p>
109:             * This implementation uses {@link File#delete()}.
110:             *
111:             * @param fileToDelete  the file to delete, exists, not null
112:             * @return true if the file was deleteds
113:             * @throws NullPointerException if the file is null
114:             * @throws IOException if an error occurs during file deletion
115:             */
116:            protected boolean doDelete(File fileToDelete) throws IOException {
117:                return fileToDelete.delete();
118:            }
119:
120:            //-----------------------------------------------------------------------
121:            /**
122:             * Gets a string describing the delete strategy.
123:             *
124:             * @return a string describing the delete strategy
125:             */
126:            public String toString() {
127:                return "FileDeleteStrategy[" + name + "]";
128:            }
129:
130:            //-----------------------------------------------------------------------
131:            /**
132:             * Force file deletion strategy.
133:             */
134:            static class ForceFileDeleteStrategy extends FileDeleteStrategy {
135:                /** Default Constructor */
136:                ForceFileDeleteStrategy() {
137:                    super ("Force");
138:                }
139:
140:                /**
141:                 * Deletes the file object.
142:                 * <p>
143:                 * This implementation uses <code>FileUtils.forceDelete() <code>
144:                 * if the file exists.
145:                 *
146:                 * @param fileToDelete  the file to delete, not null
147:                 * @return Always returns <code>true</code>
148:                 * @throws NullPointerException if the file is null
149:                 * @throws IOException if an error occurs during file deletion
150:                 */
151:                protected boolean doDelete(File fileToDelete)
152:                        throws IOException {
153:                    FileUtils.forceDelete(fileToDelete);
154:                    return true;
155:                }
156:            }
157:
158:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.