I’m trying to display whats in the file header which should be text (the rest of the file is binary) but when I print strtemp I get this:
strTemp: ??????
Here is the code.
String fileName = "test.file";
URI logUri = new File(fileName).getAbsoluteFile().toURI();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(logUri)));
byte[] btemp = new byte[14];
in.read(btemp);
String strtemp = "";
for(int i = 0; i < btemp.length; i+= 2) {
strtemp += String.valueOf((char)(((btemp[i]&0x00FF)<<8) + (btemp[i+1]&0x00FF)));
}
System.out.println("strTemp: " + strtemp);
How do I get strtemp to be whats in the file? and to display it properly?
As you can see from the Constructor summary of http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html you can initialize a String from bytes directly.
Also you should supply the charset you have in your source file.