1 /* $Id: Sed.java 1 2010-02-24 13:03:05Z haidw $ */
\r
4 import java.io.BufferedReader;
\r
5 import java.io.BufferedWriter;
\r
7 import java.io.FileInputStream;
\r
8 import java.io.FileWriter;
\r
9 import java.io.IOException;
\r
10 import java.io.InputStreamReader;
\r
13 * Class to find and replace strings in a files of a directory.
\r
14 * The intention is to get a behavior similar to UNIX's sed (stream
\r
20 * In the given file, search for occurences of the given search
\r
21 * pattern and replace it by the given replacement.
\r
22 * searchPattern, replacementPattern will be used in a call of
\r
23 * String.replaceAll(searchPattern, replacementPattern).
\r
25 * @param filename file to search
\r
26 * @param searchPattern regular expression to search for
\r
27 * @param replacementPattern replacement
\r
29 protected void readReplace(String filename, String searchPattern,
\r
30 String replacementPattern) throws IOException {
\r
32 StringBuffer buffer = new StringBuffer();
\r
33 FileInputStream fileInputStream = new FileInputStream(filename);
\r
34 BufferedReader reader = new BufferedReader(
\r
35 new InputStreamReader(fileInputStream));
\r
36 while((line = reader.readLine()) != null) {
\r
37 String newline = line.replaceAll(searchPattern, replacementPattern);
\r
39 if (!newline.equals(line)) {
\r
40 System.out.println("Found pattern in " + filename
\r
41 + ". New line: " + newline);
\r
44 buffer.append(newline + "\n");
\r
47 BufferedWriter out = new BufferedWriter(new FileWriter(filename));
\r
48 out.write(buffer.toString());
\r
53 * Iterate through the given directory tree. For each file found in
\r
54 * the file, the method
\r
55 * {@link #processFile(java.lang.String)} is called.
\r
57 * @param path the path of the directory to be browsed
\r
58 * @param currentDir the subdirectory currently explored
\r
60 protected void browseDirectoryTree(String path)
\r
61 throws IOException {
\r
62 File file = new File(path);
\r
64 if (!file.exists()) return;
\r
65 if (!file.isDirectory()) return;
\r
67 //loop through files in directory
\r
68 String[] files = file.list();
\r
70 for (int k = 0; k < files.length; k++) {
\r
71 File newfile = new File(file.getPath(), files[k]);
\r
72 if (newfile.isFile())
\r
73 readReplace(path + System.getProperty("file.separator")
\r
74 + files[k], _searchPattern, _replacementPattern);
\r
75 else if (newfile.isDirectory()) {
\r
76 browseDirectoryTree(file.getPath()
\r
77 + System.getProperty("file.separator")
\r
84 * In the given file or in the files located in the given directory,
\r
85 * search for occurences of the given search pattern and replace it
\r
86 * by the given replacement. searchPattern, replacementPattern will
\r
87 * be used in a call of
\r
88 * String.replaceAll(searchPattern, replacementPattern).
\r
90 * @param path file or path to search
\r
91 * @param searchPattern regular expression to search for
\r
92 * @param replacementPattern replacement
\r
94 public void sed(String path, String searchPattern,
\r
95 String replacementPattern) throws IOException {
\r
96 File file = new File(path);
\r
97 _searchPattern = searchPattern;
\r
98 _replacementPattern = replacementPattern;
\r
99 if (!file.exists()) return;
\r
100 if (file.isFile()) {
\r
101 readReplace(path, searchPattern, replacementPattern);
\r
103 else if (file.isDirectory()) {
\r
104 browseDirectoryTree(path);
\r
109 * Test the implementation.
\r
111 * @param args args[0] file or directory to search
\r
112 * args[1] regular expression to search for
\r
113 * args[2] replacement
\r
115 public static void main(String args[]) {
\r
116 if (args.length == 3) {
\r
118 new Sed().sed(args[0], args[1], args[2]);
\r
120 catch (IOException e) {
\r
121 System.out.println("Sed: An error occured:");
\r
122 System.out.println(e.getMessage());
\r
126 System.out.println("usage: java Sed directory|file "
\r
127 + "searchPattern replacementPattern");
\r
131 String _searchPattern = "";
\r
132 String _replacementPattern = "";
\r