The easiest way is to do the right thing: use Reader to read text data:
String content = "";
Reader in = new InputStreamReader(this.getClass().getResourceAsStream("asdf.txt"), THE_ENCODING);
StringBuffer temp = new StringBuffer(1024);
char[] buffer = new char[1024];
int read;
while ((read=in.read(buffer, 0, buffer.len)) != -1) {
temp.append(buffer, 0, read);
}
content = temp.toString().
Not that you specifically determine the encoding of the text file you want to read. In the above example, this will be THE_ENCODING.
And note that both your code and this sample code work equally well in Java SE and J2ME.
source
share