Here is some methods to read file to byte array that I use in my android app.
- M1:
private static byte[] readFileAsBytes(String filePath)
throws java.io.IOException{
FileInputStream fisTargetFile = new FileInputStream(new File(filePath));
String targetFileStr = IOUtils.toString(fisTargetFile, "UTF-8");
byte[] inputData = IOUtils.toByteArray(new StringReader(targetFileStr),"UTF-8");
return inputData;
}
- M2
private static byte[] readFileAsBytes(String filePath)
throws java.io.IOException{
File file = new File(filePath);
FileInputStream inputFile = new FileInputStream(file);
byte inputData[] = new byte[(int)file.length()];
inputFile.read(inputData);
inputFile.close();
return inputData;
}
I also use method in this ...
But, when I debug, I dectect some redundant bytes, example:
File text: ABCDEF
When debug:
- In M1: inputData: {-17,-69,-65,65,66,67,68,69,70}
I know A -> 65, B -> 66,... But Why appear {-17,-69,-65}
-In M2: Appear many redundancy than M1.
I have searched, but not find same problem.
Any suggestions for me.
Thanks!!!