View Javadoc

1   /*
2    *  File: MiscUtil.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.misc;
12  
13  import java.io.File;
14  import java.io.FileInputStream;
15  import java.io.FileOutputStream;
16  import java.io.FileReader;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.io.OutputStream;
20  
21  /**
22   * Some static helper methods (mainly concerned with String manipulation).
23   * 
24   * @author Peter Kliem
25   * @version $Id: MiscUtil.java 250 2007-02-12 00:15:49Z olk $
26   */
27  public class MiscUtil {
28      /**
29       * Retrieve the filename from a complete path string.
30       * 
31       * @param path path including filename (path separator "/" or "\")
32       * @return filename (last part of the path)
33       */
34      public static String getFilename(String path) {
35          int idx = path.lastIndexOf("\\");
36          if (idx == -1) {
37              idx = path.lastIndexOf("/");
38          }
39          if (idx == -1) {
40              return path;
41          } else {
42              return path.substring(idx + 1);
43          }
44      }
45  
46      /**
47       * Extract path from a complete path including filename.
48       * 
49       * @param path path including filename (path separator "/" or "\")
50       * @return path (path without last element)
51       */
52      public static String getPath(String path) {
53          int idx = path.lastIndexOf("\\");
54          if (idx == -1) {
55              idx = path.lastIndexOf("/");
56          }
57          if (idx == -1) {
58              return path;
59          } else {
60              return path.substring(0, idx);
61          }
62  
63      }
64  
65      /**
66       * Read a textfile into a StringBuffer.
67       * 
68       * @param completePath complete path of the file
69       * @return StringBuffer
70       */
71      public static StringBuffer readTextFile(String completePath) {
72          StringBuffer buf = new StringBuffer();
73          File file = new File(completePath);
74          try {
75              FileReader fr = new FileReader(file);
76              char buffer[] = new char[1024];
77              int read = 1;
78              while (read > 0) {
79                  read = fr.read(buffer);
80                  if (read > 0) {
81                      buf.append(buffer, 0, read);
82                  }
83              }
84              fr.close();
85          } catch (Exception e) {
86              e.printStackTrace();
87              throw new RuntimeException("File could not be read " + completePath + " " + e.getLocalizedMessage());
88          }
89          return buf;
90      }
91  
92      public static void copyFile(File srcFile, File destFile) throws IOException {
93          InputStream src = new FileInputStream(srcFile);
94          OutputStream dest = new FileOutputStream(destFile);
95          byte buffer[] = new byte[1024];
96          int read = 1;
97          while (read > 0) {
98              read = src.read(buffer);
99              if (read > 0) {
100                 dest.write(buffer, 0, read);
101             }
102         }
103         src.close();
104         dest.close();
105     }
106 
107     /**
108      * Removes leading whitespaces.
109      * 
110      * @param str String to treat
111      * @return str without leading whitespaces
112      */
113     public static String leftTrim(String str) {
114         if (str.length() == 0) {
115             return str;
116         }
117         int i = 0;
118         while (i < str.length()) {
119             char c = str.charAt(i);
120             if (Character.isWhitespace(c)) {
121                 i++;
122             } else {
123                 break;
124             }
125         }
126         return str.substring(i);
127     }
128 
129     /**
130      * Removes trailing whitespaces.
131      * 
132      * @param str String to treat
133      * @return str without trailing whitespaces
134      */
135     public static String rightTrim(String str) {
136         if (str.length() == 0) {
137             return str;
138         }
139         int i = str.length() - 1;
140         while (i >= 0) {
141             char c = str.charAt(i);
142             if (Character.isWhitespace(c)) {
143                 i--;
144             } else {
145                 break;
146             }
147         }
148         return str.substring(0, i + 1);
149     }
150 
151 }