1
2
3
4
5
6
7
8
9
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
23
24
25
26
27 public class MiscUtil {
28
29
30
31
32
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
48
49
50
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
67
68
69
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
109
110
111
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
131
132
133
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 }