1 /*****************************************************************************
5 * Creation date 21.03.2013
8 * @copyright OPEN CASCADE 2012
9 *****************************************************************************/
10 package org.splat.util;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.io.Reader;
19 import java.io.Writer;
23 * Class for working with files.
25 public final class IOUtils {
28 * Private constructor.
35 * Copy a source stream to a target stream using a buffer of 1024 bytes.
43 * if reading or writing is failed
45 static public void copy(final InputStream src, final OutputStream target)
48 byte[] buffer = new byte[1024];
50 for (int i = src.read(buffer); i != -1; i = src.read(buffer)) {
51 target.write(buffer, 0, i);
58 * Copy a source stream to a target file defined by its path.
62 * @param targetFilePath
65 * if reading or writing is failed
67 static public void copy(final InputStream src, final String targetFilePath)
70 FileOutputStream outputStream = new FileOutputStream(targetFilePath);
73 copy(src, outputStream);
83 * Copy a Reader to a Writer using a buffer of 1024.
90 * if reading or writing is failed
92 static public void copy(final Reader src, final Writer res)
95 char[] buffer = new char[1024];
97 for (int i = src.read(buffer); i != -1; i = src.read(buffer)) {
98 res.write(buffer, 0, i);
105 * Copy a source file to a target file.
111 * @throws IOException
112 * if reading or writing is failed
114 static public void copy(final File src, final File target)
117 FileInputStream inputStream = new FileInputStream(src);
120 FileOutputStream outputStream = new FileOutputStream(target);
123 copy(inputStream, outputStream);
124 outputStream.flush();
127 outputStream.close();
136 * Copy a source file to a target file defined by their paths.
142 * @throws IOException
143 * if reading or writing is failed
145 static public void copy(final String srcPath, final String resPath)
148 FileInputStream inputStream = new FileInputStream(srcPath);
151 FileOutputStream outputStream = new FileOutputStream(resPath);
154 copy(inputStream, outputStream);
155 outputStream.flush();
158 outputStream.close();
167 * Copy data from URL to a file.
173 * @throws IOException
174 * if reading or writing is failed
176 static public void copyURLtoFile(final String srcUrl,
177 final String resFilePath) throws IOException {
179 URL url = new URL(srcUrl);
181 InputStream inputStream = url.openStream();
184 FileOutputStream res = new FileOutputStream(resFilePath);
187 IOUtils.copy(inputStream, res);
200 * Delete the content of the folder or delete the file. If it is a folder <BR>
201 * then delete all children before.
204 * the file or folder to delete
205 * @return return true if succeeded, otherwise return false
207 static public boolean delete(final File file) {
208 if (file.isDirectory()) {
209 for (File child : file.listFiles()) {
213 return file.delete();