public static String readStringFromFile(String path) {
if (log.isTraceEnabled())
log.trace("readStringFromFile: " + path);
File f = new File(path);
BufferedReader input;
try {
input = new BufferedReader(new FileReader(f));
StringWriter writer = new StringWriter();
IOUtils.copyWriter(input, writer);
String contents = writer.toString();
return contents;
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found: " + path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void copyWriter(BufferedReader input, StringWriter writer)
throws IOException {
char[] cbuf = new char[1024];
int read = 0;
while ((read = input.read(cbuf)) != -1) {
writer.write(cbuf, 0, read);
}
}
2013年3月25日 09:22