source: src/main/java/weka/gui/scripting/ScriptUtils.java @ 24

Last change on this file since 24 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 5.2 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * ScriptUtils.java
19 * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui.scripting;
23
24import java.io.BufferedReader;
25import java.io.BufferedWriter;
26import java.io.File;
27import java.io.FileInputStream;
28import java.io.FileOutputStream;
29import java.io.FileReader;
30import java.io.FileWriter;
31import java.io.IOException;
32import java.io.InputStream;
33import java.io.OutputStream;
34
35/**
36 * A helper class for Script related stuff.
37 *
38 * @author  fracpete (fracpete at waikato dot ac dot nz)
39 * @version $Revision: 5928 $
40 */
41public class ScriptUtils {
42 
43  /**
44   * Copies or moves files and directories (recursively).
45   * If targetLocation does not exist, it will be created.
46   * <p/>
47   * Original code from <a href="http://www.java-tips.org/java-se-tips/java.io/how-to-copy-a-directory-from-one-location-to-another-loc.html" target="_blank">Java-Tips.org</a>.
48   *
49   * @param sourceLocation      the source file/dir
50   * @param targetLocation      the target file/dir
51   * @param move                if true then the source files/dirs get deleted
52   *                            as soon as copying finished
53   * @throws IOException        if copying/moving fails
54   */
55  protected static void copyOrMove(File sourceLocation, File targetLocation, boolean move) throws IOException {
56    String[]            children;
57    int                 i;
58    InputStream         in;
59    OutputStream        out;
60    byte[]              buf;
61    int                 len;
62   
63    if (sourceLocation.isDirectory()) {
64      if (!targetLocation.exists())
65        targetLocation.mkdir();
66
67      children = sourceLocation.list();
68      for (i = 0; i < children.length; i++) {
69        copyOrMove(
70            new File(sourceLocation, children[i]),
71            new File(targetLocation, children[i]),
72            move);
73      }
74     
75      if (move)
76        sourceLocation.delete();
77    }
78    else {
79      in = new FileInputStream(sourceLocation);
80      // do we need to append the filename?
81      if (targetLocation.isDirectory())
82        out = new FileOutputStream(targetLocation.getAbsolutePath() + File.separator + sourceLocation.getName());
83      else
84        out = new FileOutputStream(targetLocation);
85
86      // Copy the content from instream to outstream
87      buf = new byte[1024];
88      while ((len = in.read(buf)) > 0)
89        out.write(buf, 0, len);
90     
91      in.close();
92      out.close();
93     
94      if (move)
95        sourceLocation.delete();
96    }
97  }
98 
99  /**
100   * Copies the file/directory (recursively).
101   *
102   * @param sourceLocation      the source file/dir
103   * @param targetLocation      the target file/dir
104   * @throws IOException        if copying fails
105   */
106  public static void copy(File sourceLocation, File targetLocation) throws IOException {
107    copyOrMove(sourceLocation, targetLocation, false);
108  }
109 
110  /**
111   * Moves the file/directory (recursively).
112   *
113   * @param sourceLocation      the source file/dir
114   * @param targetLocation      the target file/dir
115   * @throws IOException        if moving fails
116   */
117  public static void move(File sourceLocation, File targetLocation) throws IOException {
118    copyOrMove(sourceLocation, targetLocation, true);
119  }
120 
121  /**
122   * Saves the content to a file.
123   *
124   * @param file                the file to save to
125   * @param content             the content to save
126   * @return                    true if successfully saved
127   */
128  public static boolean save(File file, String content) {
129    boolean             result;
130    BufferedWriter      writer;
131   
132    writer = null;
133    try {
134      writer = new BufferedWriter(new FileWriter(file));
135      writer.write(content);
136      writer.flush();
137      result = true;
138    }
139    catch (Exception e) {
140      e.printStackTrace();
141      result = false;
142    }
143    finally {
144      if (writer != null) {
145        try {
146          writer.close();
147        }
148        catch (Exception e) {
149          // ignored
150        }
151      }
152    }
153   
154    return result;
155  }
156 
157  /**
158   * Tries to load the file and return its content.
159   *
160   * @param file        the file to open
161   * @return            the content, otherwise null
162   */
163  public static String load(File file) {
164    StringBuffer        result;
165    BufferedReader      reader;
166    String              line;
167    String              newLine;
168   
169    result  = new StringBuffer();
170    newLine = System.getProperty("line.separator");
171    reader  = null;
172    try {
173      // add new content
174      reader = new BufferedReader(new FileReader(file));
175      while ((line = reader.readLine()) != null) {
176        result.append(line);
177        result.append(newLine);
178      }
179    }
180    catch (Exception e) {
181      e.printStackTrace();
182      result = null;
183    }
184    finally {
185      if (reader != null) {
186        try {
187          reader.close();
188        }
189        catch (Exception e) {
190          // ignored
191        }
192      }
193    }
194   
195    return ((result != null) ? result.toString() : null);
196  }
197}
Note: See TracBrowser for help on using the repository browser.