1 /*****************************************************************************
5 * Creation date 21.03.2013
8 * @copyright OPEN CASCADE 2012-2014
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 // Create target directory if necessary
121 if (!target.getParentFile().exists()) {
122 target.getParentFile().mkdirs();
124 FileOutputStream outputStream = new FileOutputStream(target);
127 copy(inputStream, outputStream);
128 outputStream.flush();
131 outputStream.close();
140 * Copy a source file to a target file defined by their paths.
146 * @throws IOException
147 * if reading or writing is failed
149 static public void copy(final String srcPath, final String resPath)
152 FileInputStream inputStream = new FileInputStream(srcPath);
155 FileOutputStream outputStream = new FileOutputStream(resPath);
158 copy(inputStream, outputStream);
159 outputStream.flush();
162 outputStream.close();
171 * Copy data from URL to a file.
177 * @throws IOException
178 * if reading or writing is failed
180 static public void copyURLtoFile(final String srcUrl,
181 final String resFilePath) throws IOException {
183 URL url = new URL(srcUrl);
185 InputStream inputStream = url.openStream();
188 FileOutputStream res = new FileOutputStream(resFilePath);
191 IOUtils.copy(inputStream, res);
204 * Delete the content of the folder or delete the file. If it is a folder <BR>
205 * then delete all children before.
208 * the file or folder to delete
209 * @return return true if succeeded, otherwise return false
211 static public boolean delete(final File file) {
212 if (file.isDirectory()) {
213 for (File child : file.listFiles()) {
217 return file.delete();