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 package ca.uhn.hl7v2.testpanel.util;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.io.Reader;
35 import java.nio.charset.Charset;
36
37 public class FileUtils {
38
39 @SuppressWarnings("unused")
40 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FileUtils.class);
41
42 public static String loadResourceFromClasspath(String thePath) throws IOException {
43 InputStream is = FileUtils.class.getClassLoader().getResourceAsStream(thePath);
44 return readFromReaderIntoString(new InputStreamReader(is));
45 }
46
47 public static String readFile(File file) throws FileNotFoundException, IOException {
48 return readFile(file, null);
49 }
50
51 public static String readFile(File theFile, Charset theCharset) throws IOException {
52 if (theCharset == null) {
53 theCharset = Charset.defaultCharset();
54 }
55 Reader reader = new InputStreamReader(new FileInputStream(theFile), theCharset);
56 return readFromReaderIntoString(reader);
57 }
58
59 public static String readFromReaderIntoString(Reader theReader) throws IOException {
60 int next;
61 StringBuilder b = new StringBuilder();
62 char[] buf = new char[1000];
63 while ((next = theReader.read(buf)) > 0) {
64 b.append(buf, 0, next);
65 }
66
67 String profileString = b.toString();
68 return profileString;
69 }
70
71 }