Source Code Cross Referenced for FileUtils.java in  » J2EE » Pustefix » de » schlund » pfixxml » util » 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 » J2EE » Pustefix » de.schlund.pfixxml.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file is part of PFIXCORE.
003:         *
004:         * PFIXCORE is free software; you can redistribute it and/or modify
005:         * it under the terms of the GNU Lesser General Public License as published by
006:         * the Free Software Foundation; either version 2 of the License, or
007:         * (at your option) any later version.
008:         *
009:         * PFIXCORE is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012:         * GNU Lesser General Public License for more details.
013:         *
014:         * You should have received a copy of the GNU Lesser General Public License
015:         * along with PFIXCORE; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:         *
018:         */
019:        package de.schlund.pfixxml.util;
020:
021:        import java.io.File;
022:        import java.io.FileInputStream;
023:        import java.io.FileOutputStream;
024:        import java.io.IOException;
025:        import java.io.InputStreamReader;
026:        import java.io.OutputStreamWriter;
027:
028:        /**
029:         * This class contains a set of commonly used utility methods for the files.
030:         * 
031:         * @author mleidig@schlund.de
032:         *
033:         */
034:        public class FileUtils {
035:
036:            /**
037:             * Reads a text file into a string and replaces each substring that matches the regular
038:             * expression by a given replacement. Then the changed string is stored back to the file.
039:             * 
040:             * @param file - the text file
041:             * @param encoding - the file content's encoding
042:             * @param regexp - the regular expression to match substrings
043:             * @param replacement - the replacement for matched substrings
044:             */
045:            public static void searchAndReplace(File file, String encoding,
046:                    String regexp, String replacement) {
047:                try {
048:                    String content = load(file, encoding);
049:                    content = content.replaceAll(regexp, replacement);
050:                    save(content, file, encoding);
051:                } catch (IOException x) {
052:                    throw new RuntimeException(
053:                            "Search and replace failed due to IO error", x);
054:                }
055:            }
056:
057:            /**
058:             * Read a text file into a string.
059:             * 
060:             * @param file - the text file
061:             * @param encoding - text file content's encoding
062:             * @return the file content as string
063:             * @throws IOException
064:             */
065:            public static String load(File file, String encoding)
066:                    throws IOException {
067:                FileInputStream fis = new FileInputStream(file);
068:                InputStreamReader reader = new InputStreamReader(fis, encoding);
069:                StringBuffer strBuf = new StringBuffer();
070:                char[] buffer = new char[4096];
071:                int i = 0;
072:                try {
073:                    while ((i = reader.read(buffer)) != -1)
074:                        strBuf.append(buffer, 0, i);
075:                } finally {
076:                    fis.close();
077:                }
078:                return strBuf.toString();
079:            }
080:
081:            /**
082:             * Saves a string to a text file.
083:             * 
084:             * @param fileContent - the file content string
085:             * @param file - the target file
086:             * @param encoding - the text encoding
087:             * @throws IOException
088:             */
089:            public static void save(String fileContent, File file,
090:                    String encoding) throws IOException {
091:                FileOutputStream fos = new FileOutputStream(file);
092:                OutputStreamWriter writer = new OutputStreamWriter(fos,
093:                        encoding);
094:                try {
095:                    writer.write(fileContent);
096:                    writer.flush();
097:                } finally {
098:                    fos.close();
099:                }
100:            }
101:
102:            /**
103:             * Copies files from source to destination dir. The files can be filtered by name using one
104:             * or more regular expressions (e.g. ".*gif", ".*jpg").
105:             * 
106:             * @param srcDir source directory
107:             * @param destDir destination directory
108:             * @param regexps regular expressions for file names
109:             * @throws IOException
110:             */
111:            public static void copyFiles(File srcDir, File destDir,
112:                    String... regexps) throws IOException {
113:                if (!(srcDir.exists() && srcDir.isDirectory()))
114:                    throw new IllegalArgumentException(
115:                            "Source directory doesn't exist: "
116:                                    + srcDir.getAbsolutePath());
117:                if (!(destDir.exists() && destDir.isDirectory()))
118:                    throw new IllegalArgumentException(
119:                            "Destination directory doesn't exist: "
120:                                    + destDir.getAbsolutePath());
121:                File[] files = srcDir.listFiles();
122:                for (File file : files) {
123:                    if (file.isFile()) {
124:                        String name = file.getName();
125:                        boolean matches = true;
126:                        for (String regexp : regexps) {
127:                            matches = name.matches(regexp);
128:                            if (matches)
129:                                break;
130:                        }
131:                        if (matches) {
132:                            File destFile = new File(destDir, file.getName());
133:                            copyFile(file, destFile);
134:                        }
135:                    }
136:                }
137:            }
138:
139:            /**
140:             * Copies a source file to a target file.
141:             * 
142:             * @param srcFile source file
143:             * @param destFile target file
144:             * @throws IOException
145:             */
146:            public static void copyFile(File srcFile, File destFile)
147:                    throws IOException {
148:                if (!(srcFile.exists() && srcFile.isFile()))
149:                    throw new IllegalArgumentException(
150:                            "Source file doesn't exist: "
151:                                    + srcFile.getAbsolutePath());
152:                if (destFile.exists() && destFile.isDirectory())
153:                    throw new IllegalArgumentException(
154:                            "Destination file is directory: "
155:                                    + destFile.getAbsolutePath());
156:                FileInputStream in = new FileInputStream(srcFile);
157:                FileOutputStream out = new FileOutputStream(destFile);
158:                byte[] buffer = new byte[4096];
159:                int no = 0;
160:                try {
161:                    while ((no = in.read(buffer)) != -1)
162:                        out.write(buffer, 0, no);
163:                } finally {
164:                    in.close();
165:                    out.close();
166:                }
167:            }
168:
169:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.