1

Possible Duplicate:
How to create a Java String from the contents of a file

Hi I would like to read a text file and make the message a string.

String message = StringUtils.decode(FileUtils.readFileContent(templateResource.getFile(), templateResource.getCharset(), (int) templateResource.getLength()), notification.getParams());

i'm not very good with java, how can i convert this so it would work? the patch of the file i'm trying to read is: sms-notification/info-response.1.txt

I don't want to use the decode feature perhaps as the contents of the text file are just static.

would i do something like:

String message = StringUtils.decode(FileUtils.readFileContent("sms-notification/info-response.1.txt".getFile(), templateResource.getCharset(), (int) templateResource.getLength()), notification.getParams()); ?

because that is not working.

asked Nov 19, 2010 at 15:18
1

1 Answer 1

1

Files are a stream of bytes, Strings are a stream of chars. You need to define a charset for the conversion. If you don't explicitly set this then default OS charset will be used and you might get unexpected results.

StringBuilder sb = new StringBuilder();
try {
 BufferedReader in = new BufferedReader(new InputStreamReader(
 new FileInputStream(fileName), "UTF-8"));
 char[] buf = new char[1024];
 while ((int len = in.read(buf, 0, buf.length)) > 0) {
 sb.append(buf, 0, len);
 }
 in.close();
} catch (IOException e) {
}
sb.toString();
answered Nov 19, 2010 at 15:44
1
  • Hmm, you are right. Post corrected. Commented Nov 19, 2010 at 17:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.