I have some files generated from a script that provide information about various computers. The txt files are in UTF-8, however, there is one line that is in UTF-16 format. How should I go about reading this line from the file?
P.S. I'm trying to write a program to parse out all of these files and recompile them into one collective .csv file.
I have tried reading the file with a bufferedReader
and Scanner
, however this one line is the only one I am having trouble with. Most of the code I have found online for reading UTF-16 is for the entire file, which is not completely in UTF-16.
//How the line looks when opened in Notepad.
S e r i a l N u m b e r 5 C G 8 X X X X X X
//How the line looks when opened in Notepad++ with "nul" values in between each character.
S e r i a l N u m b e r 5 C G 8 X X X X X X
My code can pick up parts of the string, but the format of it is on multiple lines and Java doesn't recognize the characters in between each letter or number.
These methods differ in the number of bytes they need to store a character. UTF-8 encodes a character into a binary string of one, two, three, or four bytes. UTF-16 encodes a Unicode character into a string of either two or four bytes.
UTF-16 is, obviously, more efficient for A) characters for which UTF-16 requires fewer bytes to encode than does UTF-8. UTF-8 is, obviously, more efficient for B) characters for which UTF-8 requires fewer bytes to encode than does UTF-16.
UTF-16 isn't really fixed width; some Unicode code points are one 16-bit code unit, others require two 16-bit code units — just like UTF-8 isn't fixed width; some Unicode code points require one 8-bit code units, others require two, three or even four 8-bit code units (but not five or six, despite the comment from ...
You can try like this.
File infile = new File("/someFileInutf16.txt");
FileInputStream inputStream = new FileInputStream(infile);
Reader in = new InputStreamReader(inputStream, "UTF-16");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With