I am trying to read in the content of a file to any readable form. I am using a FileInputStream to read from the file to a byte array, and then am trying to convert that byte array into a String.
So far, I have tried 3 different ways:
FileInputStream inputStream = new FileInputStream(file);
byte[] clearTextBytes = new byte[(int) file.length()];
inputStream.read(clearTextBytes);
String s = IOUtils.toString(inputStream); //first way
String str = new String(clearTextBytes, "UTF-8"); //second way
String string = Arrays.toString(clearTextBytes); //third way
String[] byteValue = string.substring(1, string.length() - 1).split(",");
byte[] bytes = new byte[byteValue.length]
for(int i=0, len=bytes.length; i<len; i++){
bytes[i] = Byte.parseByte(byteValue[i].trim());
}
String newStr = new String(bytes);
When I print out each of the Strings:
1) prints out nothing, and
2 & 3) print out a lot of weird characters, such as:
PK!�Q���[Content_Types].xml �(���MO�@��&��f��]���pP<*���v
�ݏ�,_��i�I�(zi�N��}fڝ�
��h�5)�&��6Sf����c|�"�d��R�d���Eo�r��
�l�������:0Tɭ�"Э�p'䧘��tn��&� q(=X����!.���,�_�WF�L8W......
I would love any advice on how to properly convert my byte array to a String.