1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.mchaplin.commons.io;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.PrintWriter;
28 import java.util.Iterator;
29 import java.util.Vector;
30
31 import net.mchaplin.commons.WmindObject;
32
33 import org.apache.commons.io.FileUtils;
34
35 /***
36 * @author mchaplin@users.sourceforge.net
37 *
38 * $Header: /cvsroot/iky-container/iky-container/src/net/mchaplin/commons/io/IOUtils.java,v 1.1 2005/05/06 22:23:47 mchaplin Exp $Revision: 1.1 $Date:
39 */
40 public class IOUtils extends WmindObject{
41
42 public static final String PATH_SEPARATOR = "//";
43 public static final String LINE_BREAK = "\n";
44
45 /***
46 * Copy a file from a base source directory to a base target directory. File
47 * might be located in a subdirectory of source directory.
48 *
49 * @param file
50 * file to copy. Might be in a subdirectory
51 * @param sourceDir
52 * base source directory
53 * @param targetDir
54 * base target directory
55 *
56 * @return the file package
57 */
58 public static String copyToDirectory(File file, File sourceDir, File targetDir) {
59
60 String absPath = file.getAbsolutePath();
61 String relPath = absPath
62 .substring(sourceDir.getAbsolutePath().length());
63 log.debug("processing file : [" + file.getAbsolutePath() + "]");
64 int fNamePos = relPath.lastIndexOf(PATH_SEPARATOR);
65 String pkgPath = relPath.substring(0, fNamePos);
66 String targetPath = targetDir + PATH_SEPARATOR + pkgPath;
67
68 File fTargetPathDir = new File(targetPath);
69 if (!fTargetPathDir.exists()) {
70 fTargetPathDir.mkdirs();
71 }
72 try {
73 FileUtils.copyFileToDirectory(file, fTargetPathDir);
74 } catch (IOException e1) {
75 log.error("I/O exception occured while copying file ["
76 + file.getName() + "] to [" + targetDir + "]");
77 e1.printStackTrace();
78 }
79 return pkgPath;
80 }
81
82 /***
83 * Insert a String in a file
84 *
85 * @param inFile file to insert String to
86 * @param lineno position in file
87 * @param lineToBeInserted String to be inserted
88 * @param replace replace/append mode
89 *
90 */
91 public static boolean insertStringInFile(File inFile, int lineno, String lineToBeInserted, boolean replace) {
92
93 System.out.println("Inserting line ["+lineToBeInserted+"] into ["+inFile.getAbsolutePath()+"] at pos ["+lineno+"]");
94 boolean success = true;
95
96 File outFile = new File("~.tmp");
97
98 try {
99
100 FileInputStream fis = new FileInputStream(inFile);
101 BufferedReader in = new BufferedReader(new InputStreamReader(fis));
102
103 FileOutputStream fos = new FileOutputStream(outFile);
104 PrintWriter out = new PrintWriter(fos);
105 String thisLine = "";
106 int i =1;
107 while ((thisLine = in.readLine()) != null) {
108 if(i == lineno) {
109 out.println(lineToBeInserted);
110
111 }
112 if (!replace) {
113 out.println(thisLine);
114 }
115 i++;
116 }
117 out.flush();
118 in.close();
119
120 fos = new FileOutputStream(inFile);
121 out = new PrintWriter(fos);
122
123 fis = new FileInputStream(outFile);
124 in = new BufferedReader(new InputStreamReader(fis));
125 while ((thisLine = in.readLine()) != null) {
126 out.println(thisLine);
127 }
128 out.flush();
129 out.close();
130 in.close();
131 } catch (FileNotFoundException e) {
132 log.error("File ["+inFile.getAbsolutePath()+"] not found !");
133 success = false;
134 } catch (IOException e) {
135 log.error("I/O exception occured while inserting line into ["+inFile.getAbsolutePath()+"]");
136 success = false;
137 }
138 return success;
139 }
140
141
142 /***
143 * @param targetFile
144 * @param vFileContent
145 */
146 public static void loadFileToVector(File targetFile, Vector<String> vFileContent) {
147
148 FileInputStream fis = null;
149 BufferedReader in = null;
150 String sLineContent = null;
151 try {
152 fis = new FileInputStream(targetFile);
153 in = new BufferedReader(new InputStreamReader(fis));
154 } catch (FileNotFoundException e) {
155 log.error("Unable to load file [" + targetFile.getAbsolutePath()
156 + "]. File not found !");
157 }
158 try {
159
160 while ((sLineContent = in.readLine()) != null) {
161 vFileContent.add(sLineContent);
162 }
163 in.close();
164 } catch (IOException e1) {
165 log.error("I/O exception occured while reading file ["
166 + targetFile.getName() + "]");
167 }
168 }
169
170 /***
171 * @param targetFile
172 * @param vFileContent
173 */
174 public static void writeVectorToFile(File targetFile, Vector<String> vFileContent) {
175 PrintWriter out = null;
176 FileOutputStream fos = null;
177
178
179 try {
180 fos = new FileOutputStream(targetFile);
181 } catch (FileNotFoundException e2) {
182 log.error("Unable to write modified tags to file ["
183 + targetFile.getAbsolutePath() + "]. File not found !");
184 }
185 out = new PrintWriter(fos);
186 Iterator<String> itr = vFileContent.iterator();
187 while (itr.hasNext()) {
188 out.println(itr.next());
189 }
190
191 out.flush();
192 out.close();
193 }
194
195 public static int getInstanceCount(String string, String value) {
196 int offset = 0;
197 int index = string.indexOf(value);
198 while (index != -1) {
199 string = string.substring(index+value.length());
200 offset++;
201 index = string.indexOf(value);
202 }
203 return offset;
204 }
205 }