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: }
|