-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path173.java
More file actions
218 lines (180 loc) · 4.24 KB
/
Copy path173.java
File metadata and controls
218 lines (180 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package ca.carleton.gcrc.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;
import org.json.JSONObject;
import org.json.JSONTokener;
public class TextFileUtils {
static public String readTextFile(File file) throws Exception {
FileInputStream fis = null;
InputStreamReader isr = null;
try {
StringWriter sw = new StringWriter();
fis = new FileInputStream(file);
isr = new InputStreamReader(fis, "UTF-8");
StreamUtils.copyStream(isr, sw);
sw.flush();
fis.close();
fis = null;
isr.close();
isr = null;
return sw.toString();
} catch (Exception e) {
throw new Exception("Error while reading text file: "+file,e);
} finally {
if( isr != null ){
try {
isr.close();
isr = null;
} catch(Exception e) {
// Ignore
}
}
if( fis != null ){
try {
fis.close();
fis = null;
} catch(Exception e) {
// Ignore
}
}
}
}
static public PrintWriter writeTextFile(File file) throws Exception {
try {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
PrintWriter pw = null;
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
pw = new PrintWriter(osw);
return pw;
} catch (Exception e) {
throw new Exception("Error while opening text file for writing: "+file,e);
}
}
static public void writeTextFile(File file, String content) throws Exception {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(content);
osw.flush();
osw.close();
osw= null;
fos.close();
fos = null;
} catch (Exception e) {
throw new Exception("Error while writing text file: "+file,e);
} finally {
try {
if( null != osw ){
osw.close();
osw = null;
}
} catch(Exception e){
// Ignore
}
try {
if( null != fos ){
fos.close();
fos = null;
}
} catch(Exception e){
// Ignore
}
}
}
static public Properties readPropertiesFile(File file) throws Exception {
FileInputStream fis = null;
InputStreamReader isr = null;
try {
Properties props = new Properties();
fis = new FileInputStream(file);
isr = new InputStreamReader(fis, "UTF-8");
props.load(isr);
fis.close();
fis = null;
isr.close();
isr = null;
return props;
} catch(Exception e) {
throw new Exception("Error while loading properties from: "+file, e);
} finally {
if( null != fis ){
try {
fis.close();
fis = null;
} catch(Exception e) {
// Ignore
}
}
if( null != isr ){
try {
isr.close();
isr = null;
} catch(Exception e) {
// Ignore
}
}
}
}
static public void readPropertiesFile(Properties props, File file) throws Exception {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
props.store(osw, null);
fos.close();
fos = null;
osw.close();
osw = null;
} catch(Exception e) {
throw new Exception("Error while writing properties to: "+file, e);
} finally {
if( null != fos ){
try {
fos.close();
fos = null;
} catch(Exception e) {
// Ignore
}
}
if( null != osw ){
try {
osw.close();
osw = null;
} catch(Exception e) {
// Ignore
}
}
}
}
static public JSONObject readJsonObjectFile(File file) throws Exception {
try {
JSONObject json = null;
String jsonStr = TextFileUtils.readTextFile(file);
JSONTokener tokener = new JSONTokener(jsonStr);
Object o = tokener.nextValue();
if( o instanceof JSONObject ){
json = (JSONObject)o;
} else {
throw new Exception("Unexpected class: "+o.getClass().getName());
}
// Check that file is empty
if( tokener.more() ) {
throw new Exception("File contains more than the object");
}
return json;
} catch (Exception e) {
throw new Exception("Error while parsing JSON Object file: "+file);
}
}
}