How to read 2 bytes symbols in java into short?

72 Views Asked by At

So f.e. i have a file ala.xs that contains: "sąłs" and i would like to read those symbols from this file and then put them into the table of shorts and then printf them on stdout. I am having some trouble with this becouse I have to read them as single chars and then join this chars into shorts. Here is my code:

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Main {
    public static void main(String[] args) throws IOException {
        short [] words = new short[10];
        for(int i = 0 ; i < 10; i++)
            words[i] = 0;
        Reader reading = new FileReader("ala.xs");
        short character;
        int i = 0;
        while ((character = (short) reading.read()) != -1) {
            words[i] = character;
            System.out.println(words[i]);
            i++;
        }
        reading.close();
    }
}

I tried to printf them like System.out.println((char)words[i]); but this is just stupid. I had this same task to do in C and in C i just had to printf them as %c and stdout joined them automaticly (that is my theory)

2

There are 2 best solutions below

7
aled On

Assuming you are trying to read all the characters of the input file then instead of using the array of short directly concatenate the characters into a String. Using a StringBuilder is more efficient than concatenating directly to a String because Strings in Java are immutable.

It should be something like this:

char character;
StringBuilder text;
while ((character = (char) reading.read()) != -1) {
   text.append(character);
}
String words=text.toString();
0
g00se On

From my comment above, it follows that the proper container for your codes is int[]. Knowing that your encoding is UTF-8, the following should give you what you want

public static int[] fileToCodepointArray(Path p) throws IOException {
    byte[] fileContents = Files.readAllBytes(p);
    return new String(fileContents, StandardCharsets.UTF_8).codePoints().
        toArray();
}