Let's suppose I have just used a BufferedInputStream
to read the bytes of a UTF-8 encoded text file into a byte array. I know that I can use the following routine to convert the bytes to a string, but is there a more efficient/smarter way of doing this than just iterating through the bytes and converting each one?
public String openFileToString(byte[] _bytes)
{
String file_string = "";
for(int i = 0; i < _bytes.length; i++)
{
file_string += (char)_bytes[i];
}
return file_string;
}
String fileString = new String(_bytes,"UTF-8");
? – CoolBeansbyte[]
in memory and converting it vianew String(_bytes,"UTF-8")
(or even by chunks with+=
on the string) is the most efficient. Chaining InputStreams and Readers might work better, especially on large files. – Bruno